<?php

namespace App\Livewire\Admin\Sistem\Kecamatan;

use App\Models\Kecamatan;
use App\Models\Kota;
use App\Models\Negara;
use App\Models\Provinsi;
use Livewire\Attributes\Url;
use Livewire\Component;
use Livewire\WithPagination;

// Rute modul ini dikunci 'role.admin.superadmin' di routes/web.php — lihat catatan yang sama
// di App\Livewire\Admin\Sistem\Negara\Index.
class Index extends Component
{
    use WithPagination;

    public string $search = '';

    // Filter Negara & Provinsi murni alat bantu UI untuk mempersempit pilihan Kota berjenjang di
    // bawah — tidak dikirim ke query (Kecamatan tidak punya kolom id_negara/id_provinsi langsung),
    // mirror pola filterProdi di App\Livewire\Admin\JadwalUjian\Form.
    #[Url(as: 'id_negara')]
    public string $filterNegara = '';

    #[Url(as: 'id_provinsi')]
    public string $filterProvinsi = '';

    #[Url(as: 'id_kota')]
    public string $filterKota = '';

    public int $perPage = 10;

    public ?int $confirmingDeleteId = null;

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

    public function updatingFilterNegara(): void
    {
        $this->filterProvinsi = '';
        $this->filterKota = '';
        $this->resetPage();
    }

    public function updatingFilterProvinsi(): void
    {
        $this->filterKota = '';
        $this->resetPage();
    }

    public function updatingFilterKota(): void
    {
        $this->resetPage();
    }

    public function confirmDelete(int $id): void
    {
        $this->confirmingDeleteId = $id;
    }

    public function cancelDelete(): void
    {
        $this->confirmingDeleteId = null;
    }

    public function delete(): void
    {
        if (! $this->confirmingDeleteId) {
            return;
        }

        Kecamatan::findOrFail($this->confirmingDeleteId)->delete();

        $this->confirmingDeleteId = null;
        $this->resetPage();
    }

    /**
     * Sama persis dengan KecamatanController::index (dengan with=parent, karena tabel selalu
     * menampilkan nama kota/provinsi/negara induknya).
     */
    public function render()
    {
        $query = Kecamatan::query()->with('kota.provinsi.negara');

        if ($this->search !== '') {
            $query->where('nama', 'like', "%{$this->search}%");
        }

        if ($this->filterKota !== '') {
            $query->where('id_kota', (int) $this->filterKota);
        }

        $kecamatanList = $query->orderBy('nama')->paginate($this->perPage);

        $provinsiQuery = Provinsi::query()->orderBy('nama');
        if ($this->filterNegara !== '') {
            $provinsiQuery->where('id_negara', (int) $this->filterNegara);
        }

        $kotaQuery = Kota::query()->orderBy('nama');
        if ($this->filterProvinsi !== '') {
            $kotaQuery->where('id_provinsi', (int) $this->filterProvinsi);
        } elseif ($this->filterNegara !== '') {
            $kotaQuery->whereIn('id_provinsi', Provinsi::where('id_negara', (int) $this->filterNegara)->pluck('id'));
        }

        // ->extends() (bukan #[Layout] attribute) — lihat catatan di App\Livewire\Admin\Fakultas\Index::render()
        return view('livewire.admin.sistem.kecamatan.index', [
            'kecamatanList' => $kecamatanList,
            'negaraOptions' => Negara::orderBy('nama')->get(['id', 'nama']),
            'provinsiOptions' => $provinsiQuery->get(['id', 'nama']),
            'kotaOptions' => $kotaQuery->get(['id', 'nama']),
        ])->extends('layouts.web');
    }
}
