<?php

namespace Modules\Inventory\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Http\Request;
use Modules\Core\Helpers\ApiResponse;
use Modules\Core\Http\Requests\UpdateStatusRequest;
use Modules\Inventory\DTO\InventorySettingFilter;
use Modules\Inventory\Http\Requests\CategoryRequest;
use Modules\Inventory\Models\InventorySetting;
use Modules\Inventory\Services\CategoryService;
use Modules\Inventory\Transformers\CategoryResource;

class CategoryController extends Controller
{
    use AuthorizesRequests;

    public function __construct(protected CategoryService $categoryService)
    {}

    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        $this->authorize('inventorySettingView', InventorySetting::class);
        $filters = InventorySettingFilter::fromRequest($request);

        $data = $this->categoryService->list($filters);

        return (CategoryResource::collection(
            $data
        ))->additional([
            'status' => true,
            'message' => 'Categories retrieved successfully.'
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(CategoryRequest $request)
    {
        $this->authorize('inventorySettingManage', InventorySetting::class);
        $category = $this->categoryService->store($request->validated());

        return (new CategoryResource($category->load(['inventoryDepartment:id,name'])))->additional([
            'status' => true,
            'message' => 'Category created successfully.'
        ]);
    }

    /**
     * Show the specified resource.
     */
    public function show(int $id)
    {
        $this->authorize('inventorySettingView', InventorySetting::class);

        $data = $this->categoryService->show($id);

        return (new CategoryResource($data))->additional([
            'status' => true,
            'message' => 'Category retrieved successfully.'
        ]);
    }


    /**
     * Update the specified resource in storage.
     */
    public function update(CategoryRequest $request, int $id)
    {
        $this->authorize('inventorySettingManage', InventorySetting::class);

        $category = $this->categoryService->update(
            $this->categoryService->show($id),
            $request->validated()
        );

        return (new CategoryResource($category))->additional([
            'status' => true,
            'message' => 'Category updated successfully.'
        ]);
    }

    public function updateStatus(UpdateStatusRequest $request, $id)
    {
        $this->authorize('inventorySettingManage', InventorySetting::class);
        $category = $this->categoryService->show($id);

        $this->categoryService->updateStatus(
            $category,
            $request->validated()
        );

        return ApiResponse::successResponse('Category status updated successfully.');
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id) {}
}
