<?php

namespace Modules\Order\Controllers\Api;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Modules\Product\Models\Product;
use Modules\Order\Models\Order;
use Modules\Order\Models\Resources\OrderResource;
use Modules\Order\Services\OrderService;
use Modules\Order\Services\CartService;
use Modules\User\Enums\AccountStatusEnum;
use Modules\User\Enums\UserRoleEnum;
use Modules\Order\Enums\OrderStatusEnum;
use Modules\User\Services\AccountService;
use Modules\Order\Requests\Api\OrderRequest;

class OrderController extends Controller
{
    private OrderService $orderService;
    private CartService $cartService;
    private AccountService $accountService;

    public function __construct(OrderService $orderService ,CartService $cartService ,AccountService $accountService)
    {
        $this->orderService = $orderService;
        $this->cartService = $cartService;
        $this->accountService = $accountService;
    }
    
    public function index(Request $request)
    {
        $account = $this->accountService->getAccount();
        
        if($account['status'] == AccountStatusEnum::USER->value)
        {
            if($account['user']->role == UserRoleEnum::USER->value)
            {
                $request->merge(['user_status'=>$account['status'],'user_id'=>$account['user']->id]);
            }
            else
            {
                $request->merge(['store_id'=>$account['user']->store_id]);
            }
        }
        else
        {
            $request->merge(['user_status'=>$account['status'],'user_id'=>$account['user']->id]);
        }
        
        $orders = $this->orderService->get($request);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> OrderResource::collection($orders)
        ];
        return ResponseService::response($response);
    }
    
    public function show(Request $request , $id)
    {
        $order = Order::findOrFail($id);
        $order = $this->orderService->getItem($order);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> new OrderResource($order)
        ];
        return ResponseService::response($response);
    }
    public function calculate(Request $request)
    {
        $calculate = $this->cartService->calculate($request);
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> $calculate
        ];
        return ResponseService::response($response);
    }
    public function order(OrderRequest $request)
    {
        $this->orderService->create($request);
        $orders = $this->orderService->get($request);
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> OrderResource::collection($orders)
        ];
        return ResponseService::response($response);
    }
    
    public function statistics(Request $request)
    {
        $request->merge(['store_id'=>auth()->user()->store_id,'type'=>OrderStatusEnum::NEW->value]);
        $new = $this->orderService->get($request)->count();
        $request->merge(['type'=>OrderStatusEnum::SHIPPING->value]);
        $delivery = $this->orderService->get($request)->count();
        $request->merge(['type'=>OrderStatusEnum::RECEIVED->value,'typeDays'=>'today']);
        $recieved = $this->orderService->get($request)->count();
        $request->merge(['type'=>OrderStatusEnum::CANCELED->value]);
        $canceled = $this->orderService->get($request)->count();

        
        $response = [
            'status'=>200,
            'message'=>'',
            'data'=> 
            [
                'new' => $new,
                'prepare' => $prepare,
                'delivery' => $delivery,
                'recieved' => $recieved,
            ]
        ];
        return ResponseService::response($response);
    }
    
}
