<?php

namespace Modules\Product\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Modules\Canteen\Models\Canteen;
use Modules\Product\Models\Product;
use Modules\Product\Requests\Api\ProductRequest;
use Modules\Product\Resources\Api\ProductResource;
use Modules\Product\Services\ProductService;


class ProductController extends Controller
{

    public function __construct(
        private ProductService $productService,
    ) {
    }

    /**
     * Get Products.
     * @param Request $request
     * @param Canteen $canteen
     * @return JsonResponse
     */
    public function index(Request $request, Canteen $canteen): void
    {
        $credentials['canteen_id'] = $canteen->id ?? (auth()->user()->canteen->id ?? -1);
        $credentials['api'] = true;
        $credentials['category_id'] = $request->category_id;
        $query = $this->productService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ProductResource::class,
            "returnName" => "products"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Get Product.
     *
     * @param Request $request
     * @param Product $product
     * @return JsonResponse
     */
    public function show(Request $request, Product $product): JsonResponse
    {
        $category = $this->productService->getItem($product->id);

        $response = [
            'status' => 200,
            'message' => '',
            'data' => new ProductResource($category)
        ];
        return ResponseService::response($response);
    }
    /**
     * Create Product.
     *
     * @param ProductRequest $request
     * @return void
     */
    public function store(ProductRequest $request): void
    {
        $credentials = $request->only('title_ar', 'title_en', 'cost', 'count', 'image', 'category_id');
        $credentials['canteen_id'] = auth()->user()->canteen->id ?? 0;
        $this->productService->create($credentials);

        $query = $this->productService->get(['api' => true, 'canteen_id' => $credentials['canteen_id']]);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ProductResource::class,
            "returnName" => "products"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Edit Product.
     *
     * @param ProductRequest $request
     * @param Product $product
     * @return void
     */
    public function update(ProductRequest $request, Product $product): void
    {
        $credentials = $request->only('title_ar', 'title_en', 'cost', 'count', 'image', 'category_id');


        $this->productService->update($credentials, $product->id);

        $query = $this->productService->get(['api' => true, 'canteen_id' => auth()->user()->canteen->id]);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ProductResource::class,
            "returnName" => "products"
        ];
        ResponseService::fetchPainateDate($response);
    }

    /**
     * remove product.
     *
     * @param Request $request
     * @param Product $product
     * @return void
     */
    public function delete(Request $request, Product $product): void
    {
        if ($product->canteen_id !== auth()->user()->canteen->id) {
            ResponseService::responseFail(['error' => 'not_found']);
        }
        $this->productService->destroy($product->id);

        $credentials['api'] = true;
        $credentials['canteen_id'] = auth()->user()->canteen->id ?? 0;
        $query = $this->productService->get($credentials);

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => ProductResource::class,
            "returnName" => "products"
        ];
        ResponseService::fetchPainateDate($response);
    }
}
