<?php

namespace App\Http\Controllers\Web;

use App\Models\Cart;
use App\Models\Category;
use App\Models\Reservation;
use App\Models\WorkDay;
use App\Services\Interfaces\CartRepositoryInterface;
use App\Services\Interfaces\RoomRepositoryInterface;
use App\Services\Repositories\TableNumberRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
    private $view = 'web.pages.carts.';
    private $cartRepository ;
    private $tableNumberRepository ;
    private $roomRepository ;


    public function __construct(
        CartRepositoryInterface $cartRepository,
        TableNumberRepository $tableNumberRepository,
        RoomRepositoryInterface $roomRepository
    )
    {
        $this->cartRepository = $cartRepository;
        $this->roomRepository = $roomRepository;
        $this->tableNumberRepository = $tableNumberRepository;
    }


    /**
     * Show the application dashboard.
     * @return View
     */

    public function index(Request $request) :View
    {
        $title = __('web.cart');
        $carts = $this->cartRepository->get($request);
        $request->merge(['orderBy'=>'ASC']);
        $tables = $this->tableNumberRepository->get($request);
        $rooms = $this->roomRepository->get($request);
        $day = date('l');
        $work_day = WorkDay::where('day',$day)->first();
        $start_time = date('H',strtotime($work_day->start_time));
        $reservation_id = Session::get('reservation_id');
        $reservation = Reservation::onlyTrashed()->find($reservation_id);
        $room_id = $reservation ? $reservation->room_id : null;

        [$startTimes, $endTimes, $times, $fullTimes]= $work_day->time_slots;
        // dd($startTimes, $endTimes, $times);

        $title = __('web.cart');
        return view($this->view.'index', get_defined_vars());
    }
    
    public function store(Request $request)
    {
        $user = Auth::user();
        $request->merge(['user_id'=>$user->id,'plus'=>true]);
        $carts = $this->cartRepository->save($request,$request->product_id);
        $response =
            [
                'status' => 200,
                'message' => __('web.success'),
                'data'=>$carts
            ];
        return $response;
    }

    public function update(Request $request,Cart $cart)
    {
        $user = Auth::user();
        $request->merge(['user_id'=>$user->id]);

        $carts = $this->cartRepository->save($request,$cart->product_id);

        $response =
        [
            'status' => 200,
            'message' => __('web.success'),
            'data'=>$carts
        ];
        return $response;
    }
    public function destroy(Request $request,Cart $cart)
    {
        $carts = $this->cartRepository->destroy($request,$cart->id);
        $response =
            [
                'status' => 200,
                'message' => __('web.success'),
                'data'=>$carts
            ];
        return $response;
    }

    public function calculate(Request $request)
    {
        $products = $this->cartRepository->get($request);
        $calculate = $this->cartRepository->calculate($request);
        $calculate_view = view($this->view.'calculate')->with(['carts'=>$calculate])->render();
        $products = view($this->view.'products')->with(['carts'=>$products])->render();

        return
        [
            'products' => $products,
            'calculate' => $calculate_view,
            'cobon_msg' => $calculate['cobon_msg'],
            'cobon_id' => $calculate['cobon_id'],
        ];
    }
    
    public function cartCount(Request $request)
    {
        $count = $this->cartRepository->get($request)->sum('count');
        $response =
            [
                'status' => 200,
                'message' => null,
                'data'=>$count
            ];
        return $response;
    }


}
