<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\Web\CartRequest;
use App\Models\Product;
use App\Services\Currency\CurrencyService;
use App\Services\Product\Cart\CartService;
use App\Services\Auth\User\AccountService;
use App\Services\Product\Tax\TaxService;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;

class CartController extends Controller
{
    private $view = 'web.pages.cart.';

    public function index(): View
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        $title = __('web.cart');
        $cart = new CartService();
        $carts = $cart->carts()['data']['carts'];
        $total = $cart->totalCart();
        $tax = TaxService::tax($total);
        
        if($user->role == 'company')
        {
            $forward_action = route('checkout.method');
        }
        else
        {
            $forward_action = route('checkout.location');
        }
        
        return view($this->view.'cart', get_defined_vars());
    }

    public function store(CartRequest $request,$id) :JsonResponse
    {
        $user = (new AccountService())->getAccount();
        $status = $user['status'];
        $user = $user['user'];
        if($user->role == 'company')
        {
            if($user->active_account != 'active')
            {
                return ResponseService::responseFail([__('web.wait_management_order')]);
            }
        }
            
        $product = Product::find($id);
        if(!$product)
        {
            return ResponseService::notFound();
        }
        $request->merge(['product_id'=>$id,'choice_id'=>$request->choice_id]);
        $response = (new CartService())->createCart($request->all());
        return $response;
    }
    public function edit(Request $request,$id) :JsonResponse
    {
        $carts = (new CartService())->editCart($request->all(),$id);
        return $carts;
    }
    public function delete($id) :JsonResponse
    {
        $carts = (new CartService())->deleteCart($id);
        return $carts;
    }
}
