<?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\Enums\OrderStatusEnum;
use Modules\Order\Models\Resources\CartResource;
use Modules\Order\Models\Resources\OrderResource;
use Modules\Order\Requests\Api\Order\CreateRequest;
use Modules\Order\Requests\Api\Order\ShowRequest;
use Modules\Order\Services\CartService;
use Modules\Order\Services\OrderService;
use Modules\User\Services\AccountService;

class OrderController extends Controller
{

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

    /**
     * Display a listing of the countries.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request):JsonResponse
    {
        $account = $this->accountService->getAccount();

        $orders = $this->orderService->get([
            'notTypeStatus' => OrderStatusEnum::CANCELED->value,
            'search' => $request->search ?? null,
            'showLatest' => $request->showLatest ?? null,
            'user_status'=>$account['status'],
            'user_id'=>$account['user']->id
        ]);
        $canceled_orders = $this->orderService->get([
            'typeStatus' => OrderStatusEnum::CANCELED->value,
            'search' => $request->search ?? null,
            'showLatest' => $request->showLatest ?? null,
            'user_status'=>$account['status'],
            'user_id'=>$account['user']->id
        ]);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>
            [
                'valid_orders'=>OrderResource::collection($orders),
                'canceled_orders'=>OrderResource::collection($canceled_orders),
            ]
        ]);
    }

    public function create(CreateRequest $request)
    {
        $data = $request->validated();
        $data['pay_mobile'] = true;

        $this->orderService->create($data);

        $response = [
            'status' => 200,
            'message' => __('web.success'),
            'data' => ''
        ];
        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->orderService->getItem($id);
        return ResponseService::response([
            'status'=>200,
            'message'=>'',
            'data'=>new OrderResource($cart)
        ]);
    }


}
