<?php

namespace Modules\Order\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Order\Models\Resources\CartResource;
use Modules\Order\Requests\Api\Cart\CreateRequest;
use Modules\Order\Requests\Api\Cart\EditRequest;
use Modules\Order\Requests\Api\Cart\ShowRequest;
use Modules\Order\Services\CartService;
use Modules\User\Services\AccountService;

class CartController extends Controller
{

    public function __construct(
        private AccountService $accountService,
        private CartService $cartService,
    ){}

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request):JsonResponse
    {
        $account = $this->accountService->getAccount();
        $request =
        [
            'user_id'=>$account['user']->id,
            'status'=>$account['status']
        ];
        $carts = $this->cartService->get($request);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>CartResource::collection($carts)
        ]);
    }

    public function create(CreateRequest $request)
    {
        $account = $this->accountService->getAccount();
        $user = $account['user'];
        $data = $request->validated();
        $data['user_status'] = $account['status'];
        $data['user_id'] = $user->id;
        $data['warehouse_id'] = $user->address->city->warehouses->first()->warehouse_id;

        $this->cartService->store($data);
        $request =
            [
                'user_id'=>$account['user']->id,
                'status'=>$account['status']
            ];
        $carts = $this->cartService->get($request);
        $response = [
            'status' => 200,
            'message' => __('web.cart_success'),
            'data' => CartResource::collection($carts)
        ];
        return ResponseService::response($response);
    }
    /**
     * Show the form for editing the specified Country.
     *
     * @param  int $id
     * @return JsonResponse
     */
    public function show(ShowRequest $request,int $id) : JsonResponse
    {
        $cart = $this->cartService->getItem($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new CartResource($cart)
        ]);
    }

    public function edit(EditRequest $request,int $id)
    {
        $account = $this->accountService->getAccount();
        $this->cartService->update($request->validated(),$id);
        $request =
        [
            'user_id'=>$account['user']->id,
            'status'=>$account['status']
        ];
        $carts = $this->cartService->get($request);
        $response = [
            'status' => 200,
            'message' => __('web.cart_updated'),
            'data' => CartResource::collection($carts)
        ];
        return ResponseService::response($response);
    }

    public function delete(ShowRequest $request,int $id) : JsonResponse
    {
        $account = $this->accountService->getAccount();
        $this->cartService->destroy($id);
        $request =
        [
            'user_id'=>$account['user']->id,
            'status'=>$account['status']
        ];
        $carts = $this->cartService->get($request);
        $response = [
            'status' => 200,
            'message' => __('web.cart_deleted'),
            'data' => CartResource::collection($carts)
        ];
        return ResponseService::response($response);
    }

    public function calculate(Request $request)
    {
        $account = $this->accountService->getAccount();
        $data =
            [
                'user'=>$account['user'],
                'user_id'=>$account['user']->id,
                'status'=>$account['status'],
                'coupon'=>$request->coupon ?? null,
                'payment_way_id'=>$request->payment_way_id ?? null,
            ];
        $calculate = $this->cartService->calculate($data);
        unset($calculate['carts']);
        $response = [
            'status' => 200,
            'message' => '',
            'data' => $calculate
        ];
        return ResponseService::response($response);
    }

}
