<?php

namespace App\Http\Controllers\API\Admin;

use App\Http\Controllers\API\BaseApiController;
use App\Models\LicensingChecklist;
use App\Models\LicensingLegislation;
use App\Models\LicensingStep;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AdminLicensingController extends BaseApiController
{
    // ── Legislation ──────────────────────────────────────────────────────────

    public function legislationIndex(): JsonResponse
    {
        return $this->successResponse(LicensingLegislation::orderBy('sort_order')->get());
    }

    public function legislationStore(Request $request): JsonResponse
    {
        $data = $request->validate([
            'law_number'  => 'required|string|max:50',
            'title'       => 'required|string|max:255',
            'description' => 'required|string',
            'category'    => 'required|in:Investimento,Regulação,Empresas,Fiscal,ZEE,Licenciamento',
            'icon_name'   => 'nullable|string|max:50',
            'url'         => 'nullable|url',
            'sort_order'  => 'integer|min:0',
            'is_active'   => 'boolean',
        ]);
        $item = LicensingLegislation::create($data);
        return $this->successResponse($item, 'Legislação criada', 201);
    }

    public function legislationUpdate(Request $request, int $id): JsonResponse
    {
        $item = LicensingLegislation::findOrFail($id);
        $item->update($request->only([
            'law_number', 'title', 'description', 'category', 'icon_name', 'url', 'sort_order', 'is_active',
        ]));
        return $this->successResponse($item, 'Legislação actualizada');
    }

    public function legislationDestroy(int $id): JsonResponse
    {
        LicensingLegislation::findOrFail($id)->delete();
        return $this->successResponse(null, 'Legislação removida');
    }

    // ── Steps ─────────────────────────────────────────────────────────────────

    public function stepsIndex(): JsonResponse
    {
        return $this->successResponse(LicensingStep::orderBy('sort_order')->get());
    }

    public function stepsStore(Request $request): JsonResponse
    {
        $data = $request->validate([
            'step_number'  => 'required|integer|min:1',
            'title'        => 'required|string|max:255',
            'description'  => 'required|string',
            'duration'     => 'nullable|string|max:100',
            'platform'     => 'nullable|string|max:100',
            'platform_url' => 'nullable|url',
            'sort_order'   => 'integer|min:0',
            'is_active'    => 'boolean',
        ]);
        $item = LicensingStep::create($data);
        return $this->successResponse($item, 'Passo criado', 201);
    }

    public function stepsUpdate(Request $request, int $id): JsonResponse
    {
        $item = LicensingStep::findOrFail($id);
        $item->update($request->only([
            'step_number', 'title', 'description', 'duration', 'platform', 'platform_url', 'sort_order', 'is_active',
        ]));
        return $this->successResponse($item, 'Passo actualizado');
    }

    public function stepsDestroy(int $id): JsonResponse
    {
        LicensingStep::findOrFail($id)->delete();
        return $this->successResponse(null, 'Passo removido');
    }

    // ── Checklist ─────────────────────────────────────────────────────────────

    public function checklistIndex(): JsonResponse
    {
        return $this->successResponse(LicensingChecklist::orderBy('sort_order')->get());
    }

    public function checklistStore(Request $request): JsonResponse
    {
        $data = $request->validate([
            'item_text'  => 'required|string|max:255',
            'sort_order' => 'integer|min:0',
            'is_active'  => 'boolean',
        ]);
        $item = LicensingChecklist::create($data);
        return $this->successResponse($item, 'Item criado', 201);
    }

    public function checklistUpdate(Request $request, int $id): JsonResponse
    {
        $item = LicensingChecklist::findOrFail($id);
        $item->update($request->only(['item_text', 'sort_order', 'is_active']));
        return $this->successResponse($item, 'Item actualizado');
    }

    public function checklistDestroy(int $id): JsonResponse
    {
        LicensingChecklist::findOrFail($id)->delete();
        return $this->successResponse(null, 'Item removido');
    }
}
