<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Modules\Order\Models\Cart;
use Modules\Order\Services\CartService;
use Modules\User\Models\Address;
use Modules\User\Requests\Web\AddressRequest;
use Modules\User\Services\AccountService;

class CartController extends Controller
{
    private $view = 'web.pages.cart.';
    private CartService $cartService;
    private AccountService $accountService;

    public function __construct(
        CartService $cartService,
        AccountService $accountService,
    )
    {
        $this->cartService = $cartService;
        $this->accountService = $accountService;
    }

    public function index(Request $request)
    {
        $title = __('web.cart');
        return view($this->view.'index',get_defined_vars());
    }


    public function store(Request $request)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $this->cartService->store($request,null);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }

    public function calculate(Request $request)
    {
        $url = url()->previous();

        $route = last(explode('/',$url));
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $calculate = $this->cartService->calculate($request);
        $carts = $calculate['carts'];
        $total_tax = $calculate['total_tax'];
        $total_products = $calculate['total_products'];
        $total_products_tax = $calculate['total_products_tax'];
        $shipping_cost = $calculate['shipping_cost'];
        $on_hand_cost = $calculate['on_hand_cost'];
        $discount = $calculate['discount'];
        $total = $calculate['total'];
        return view($this->view.'calculate',get_defined_vars());
    }

    public function update(Request $request,Cart $cart)
    {
        $account = $this->accountService->getAccount();
        $request->merge(['user_id'=>$account['user']->id,'status'=>$account['status']]);
        $this->cartService->update($request,$cart);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }

    public function destroy($id)
    {
        $account = $this->accountService->getAccount();
        Cart::where('id',$id)->where('status',$account['status'])->where('user_id',$account['user']->id)->firstOrFail();
        $this->cartService->destroy(request(),$id);
        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => url()->previous()
        ];
        return ResponseService::response($response);
    }
}
