<?php

namespace Modules\Order\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Order\Requests\Api\OrderRequest;
use Modules\Order\Resources\Api\OrderDetailsResource;
use Modules\Order\Resources\Api\OrderResource;
use Modules\Order\Services\OrderService;
use Modules\User\Models\User;
use Modules\User\Services\UserService;


class OrderController extends Controller
{

    public function __construct(
        private OrderService $orderService,
        private UserService $userService,
    ) {
    }

    /**
     * Get Orders.
     * @param Request $request
     * @param User $children
     * @return JsonResponse
     */
    public function index(Request $request, User $children): void
    {
        $loginAccount = auth()->user();
        if (!in_array($loginAccount->role, [4, 6])) {
            ResponseService::responseFail(['error' => 'not_found']);
        }

        if ($loginAccount->role === 4 && $children) {
            if ($children->parent_id !== auth()->id()) {
                ResponseService::responseFail(['error' => 'not_found']);
            }
            $credentials['child_id'] = $children->id;
        }
        if ($loginAccount->role === 6) {
            $credentials['canteen_id'] = $loginAccount->canteen->id ?? 0;
        }
        if (!@$credentials['child_id'] && !@$credentials['canteen_id']) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $credentials['orderBy']['key'] = "created_at";
        $credentials['orderBy']['value'] = $request->sort ?? "desc";
        $credentials['api'] = true;
        $query = $this->orderService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => OrderResource::class,
            "returnName" => "orders"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Get Products.
     * @param Request $request
     * @param User $children
     * @return JsonResponse
     */
    public function products(Request $request, User $children): void
    {
        if ($children->parent_id !== auth()->id()) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $childId = $children->id;

        $query = $this->orderService->getProductsByChildId($childId);
        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => OrderDetailsResource::class,
            "returnName" => "orderProducts"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Create Order.
     * @param OrderRequest $request
     * @return JsonResponse
     */
    public function store(OrderRequest $request)
    {
        $children = $this->userService->getItem($request->child_id);

        if ($children->school->canteen->id !== auth()->user()->canteen->id) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $credentials = $request->only('child_id', 'products');
        $credentials['children'] = $children;
        $order = $this->orderService->create($credentials);
        $response = [
            'status' => 200,
            'message' => '',
            'data' => new OrderResource($order)
        ];
        return ResponseService::response($response);
    }


}
