<?php

namespace App\Livewire;

use Livewire\Component;
use Livewire\WithPagination;
use App\Models\Assegnazione;
use App\Models\AnnoTeocratico;
use App\Models\Campagna;
use App\Models\Proclamatore;
use App\Models\Territorio;
use App\Models\Zona;
use App\Models\Tipologia;

class Registro extends Component
{
    use WithPagination;

    public string $search = '';
    public string $filtroAnno = '';
    public string $filtroZona = '';
    public string $filtroTipologia = '';
    public string $filtroStato = ''; // aperte, chiuse
    public string $sortField = 'assigned_at';
    public string $sortDirection = 'desc';

    // ─── Modal create/edit ──────────────────────────────────────
    public bool $showModal = false;
    public ?int $editingId = null;

    public string $form_territorio_id = '';
    public string $form_proclamatore_id = '';
    public string $form_anno_id = '';
    public string $form_assigned_at = '';
    public string $form_returned_at = '';
    public bool $form_counted_in_campaign = false;
    public string $form_campaign_id = '';
    public string $form_note = '';

    // ─── Delete confirm ─────────────────────────────────────────
    public bool $showDeleteConfirm = false;
    public ?int $deleteId = null;

    protected $queryString = [
        'search' => ['except' => ''],
        'filtroAnno' => ['except' => ''],
        'filtroZona' => ['except' => ''],
        'filtroTipologia' => ['except' => ''],
        'filtroStato' => ['except' => ''],
    ];

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function sortBy(string $field)
    {
        if ($this->sortField === $field) {
            $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
        } else {
            $this->sortField = $field;
            $this->sortDirection = 'desc';
        }
    }

    // ─── Admin CRUD ─────────────────────────────────────────────

    public function openCreate(): void
    {
        abort_if(!auth()->user()->can('settings.manage'), 403);
        $this->editingId = null;
        $this->form_territorio_id = '';
        $this->form_proclamatore_id = '';
        $this->form_anno_id = '';
        $this->form_assigned_at = now()->format('Y-m-d');
        $this->form_returned_at = '';
        $this->form_counted_in_campaign = false;
        $this->form_campaign_id = '';
        $this->form_note = '';
        $this->resetValidation();
        $this->showModal = true;
    }

    public function openEdit(int $id): void
    {
        abort_if(!auth()->user()->can('settings.manage'), 403);
        $a = Assegnazione::findOrFail($id);
        $this->editingId = $id;
        $this->form_territorio_id = (string) $a->territorio_id;
        $this->form_proclamatore_id = (string) $a->proclamatore_id;
        $this->form_anno_id = (string) $a->anno_teocratico_id;
        $this->form_assigned_at = $a->assigned_at?->format('Y-m-d') ?? '';
        $this->form_returned_at = $a->returned_at?->format('Y-m-d') ?? '';
        $this->form_counted_in_campaign = (bool) $a->counted_in_campaign;
        $this->form_campaign_id = (string) ($a->campaign_id ?? '');
        $this->form_note = $a->note ?? '';
        $this->resetValidation();
        $this->showModal = true;
    }

    public function save(): void
    {
        abort_if(!auth()->user()->can('settings.manage'), 403);
        $this->validate([
            'form_territorio_id'     => 'required|exists:territori,id',
            'form_proclamatore_id'   => 'required|exists:proclamatori,id',
            'form_anno_id'           => 'required|exists:anni_teocratici,id',
            'form_assigned_at'       => 'required|date',
            'form_returned_at'       => 'nullable|date|after_or_equal:form_assigned_at',
            'form_campaign_id'       => 'nullable|exists:campagne,id',
        ]);

        $data = [
            'territorio_id'       => $this->form_territorio_id,
            'proclamatore_id'     => $this->form_proclamatore_id,
            'anno_teocratico_id'  => $this->form_anno_id,
            'assigned_at'         => $this->form_assigned_at,
            'returned_at'         => $this->form_returned_at ?: null,
            'counted_in_campaign' => $this->form_counted_in_campaign,
            'campaign_id'         => $this->form_campaign_id ?: null,
            'note'                => $this->form_note ?: null,
        ];

        if ($this->editingId) {
            Assegnazione::findOrFail($this->editingId)->update($data);
        } else {
            $data['created_by'] = auth()->id();
            Assegnazione::create($data);
        }

        $this->showModal = false;
    }

    public function askDelete(int $id): void
    {
        abort_if(!auth()->user()->can('settings.manage'), 403);
        $this->deleteId = $id;
        $this->showDeleteConfirm = true;
    }

    public function deleteConfirmed(): void
    {
        abort_if(!auth()->user()->can('settings.manage'), 403);
        if ($this->deleteId) {
            Assegnazione::findOrFail($this->deleteId)->delete();
        }
        $this->deleteId = null;
        $this->showDeleteConfirm = false;
    }

    public function render()
    {
        $query = Assegnazione::with(['territorio.zona', 'proclamatore', 'annoTeocratico', 'campagna']);

        if ($this->filtroAnno) {
            $query->where('anno_teocratico_id', $this->filtroAnno);
        }

        if ($this->filtroStato === 'aperte') {
            $query->aperte();
        } elseif ($this->filtroStato === 'chiuse') {
            $query->chiuse();
        }

        if ($this->filtroZona) {
            $query->whereHas('territorio', fn($q) => $q->where('zona_id', $this->filtroZona));
        }

        if ($this->filtroTipologia) {
            $query->whereHas('territorio', fn($q) => $q->where('tipologia_id', $this->filtroTipologia));
        }

        $query->orderBy($this->sortField, $this->sortDirection);

        // In-memory search for encrypted proclamatore fields / territorio numero
        if ($this->search !== '') {
            $all = $query->get();
            $filtered = $all->filter(function ($a) {
                $haystack = mb_strtolower(
                    ($a->territorio?->numero ?? '') . ' ' .
                    ($a->proclamatore?->nome ?? '') . ' ' .
                    ($a->proclamatore?->cognome ?? '')
                );
                return str_contains($haystack, mb_strtolower($this->search));
            });

            $page = $this->getPage();
            $perPage = 25;
            $items = $filtered->slice(($page - 1) * $perPage, $perPage)->values();
            $assegnazioni = new \Illuminate\Pagination\LengthAwarePaginator(
                $items, $filtered->count(), $perPage, $page,
                ['path' => request()->url()]
            );
        } else {
            $assegnazioni = $query->paginate(25);
        }

        return view('livewire.registro', [
            'assegnazioni' => $assegnazioni,
            'anni' => AnnoTeocratico::orderByDesc('start_date')->get(),
            'zone' => Zona::attive()->orderBy('nome')->get(),
            'tipologie' => Tipologia::orderBy('nome')->get(),
            'territori' => Territorio::attivi()->orderBy('numero')->get(),
            'proclamatori' => Proclamatore::attivi()->orderBy('cognome')->orderBy('nome')->get(),
            'campagne' => Campagna::orderByDesc('created_at')->get(),
        ]);
    }
}
