<?php

namespace Modules\Category\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\Response\ResponseService;
use Illuminate\Http\Request;

use Modules\Canteen\Models\Canteen;
use Modules\Category\Models\Category;
use Modules\Category\Requests\Api\CategoryRequest;
use Modules\Category\Resources\Api\CategoryResource;
use Modules\Category\Services\CategoryService;
use Symfony\Component\HttpFoundation\JsonResponse;
class CategoryController extends Controller
{

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

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

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

    /**
     * Create Category.
     *
     * @param CategoryRequest $request
     * @return void
     */
    public function store(CategoryRequest $request): void
    {
        $credentials = $request->only('title_ar', 'title_en');
        $credentials['canteen_id'] = auth()->user()->canteen->id ?? 0;
        $this->categoryService->create($credentials);

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

        $response = [
            "query" => $query,
            "request" => $request,
            "resourceClass" => CategoryResource::class,
            "returnName" => "categories"
        ];
        ResponseService::fetchPainateDate($response);
    }
    /**
     * Edit Category.
     *
     * @param CategoryRequest $request
     * @param Category $category
     * @return void
     */
    public function update(CategoryRequest $request, Category $category): void
    {
        $credentials = $request->only('title_ar', 'title_en');

        $this->categoryService->update($credentials, $category->id);

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

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

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

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

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

}
