<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Concerns\DestroysCatalogItem;
use App\Http\Requests\StoreGarmentTypeRequest;
use App\Http\Requests\UpdateGarmentTypeRequest;
use App\Models\GarmentType;
use App\Models\LoadType;
use App\Support\CatalogCodeGenerator;
use App\Support\CatalogDeletionGuard;
use Illuminate\Http\JsonResponse;

class GarmentTypeAdminController extends Controller
{
    use DestroysCatalogItem;
    public function index(): JsonResponse
    {
        $this->authorize('viewAny', GarmentType::class);

        $rows = GarmentType::query()
            ->with('defaultLoadType:id,code,name')
            ->orderBy('sort_order')
            ->get();

        $loadTypes = LoadType::query()
            ->orderBy('sort_order')
            ->get(['id', 'code', 'name', 'sort_order']);

        return response()->json([
            'data' => $rows,
            'meta' => [
                'load_types' => $loadTypes,
            ],
            'errors' => null,
        ], JsonResponse::HTTP_OK);
    }

    public function store(StoreGarmentTypeRequest $request): JsonResponse
    {
        $data = $request->validated();
        $garmentType = GarmentType::query()->create([
            'code' => CatalogCodeGenerator::resolve(
                $data['code'] ?? null,
                $data['name'],
                GarmentType::class,
                'code',
                'prenda',
            ),
            'name' => $data['name'],
            'sort_order' => $data['sort_order'] ?? 0,
            'is_active' => $data['is_active'] ?? true,
            'default_load_type_id' => $data['default_load_type_id'] ?? null,
        ])->load('defaultLoadType:id,code,name');

        return response()->json([
            'data' => $garmentType,
            'meta' => null,
            'errors' => null,
        ], JsonResponse::HTTP_CREATED);
    }

    public function update(UpdateGarmentTypeRequest $request, GarmentType $garmentType): JsonResponse
    {
        $data = $request->validated();
        $garmentType->fill($data);
        $garmentType->save();
        $garmentType->load('defaultLoadType:id,code,name');

        return response()->json([
            'data' => $garmentType,
            'meta' => null,
            'errors' => null,
        ], JsonResponse::HTTP_OK);
    }

    public function disable(GarmentType $garmentType): JsonResponse
    {
        $this->authorize('update', $garmentType);
        $garmentType->is_active = false;
        $garmentType->save();
        $garmentType->load('defaultLoadType:id,code,name');

        return response()->json([
            'data' => $garmentType,
            'meta' => null,
            'errors' => null,
        ], JsonResponse::HTTP_OK);
    }

    public function destroy(GarmentType $garmentType): JsonResponse
    {
        $this->authorize('delete', $garmentType);

        return $this->destroyCatalogItem(
            CatalogDeletionGuard::garmentType($garmentType),
            fn () => $garmentType->delete(),
        );
    }
}
