<?php

namespace App\Livewire\Academic;

use App\Models\AcademicYear;
use App\Services\AcademicCalendarService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;

class AcademicCalendarIndex extends Component
{
    use AuthorizesRequests;

    public string $tab = 'holidays'; // 'years', 'holidays', 'events'

    // Modal state
    public bool $showYearModal = false;
    public bool $showHolidayModal = false;
    public bool $showEventModal = false;

    // Form Year
    public string $year_name = '';
    public string $year_start_date = '';
    public string $year_end_date = '';
    public bool $year_is_active = true;

    // Form Holiday
    public ?int $holiday_academic_year_id = null;
    public string $holiday_name = '';
    public string $holiday_date = '';
    public string $holiday_type = 'national'; // national, school
    public string $holiday_description = '';

    // Form Event
    public ?int $event_academic_year_id = null;
    public string $event_title = '';
    public string $event_type = 'khusus';
    public string $event_start_date = '';
    public string $event_end_date = '';
    public string $event_description = '';

    public function setTab(string $tab): void
    {
        $this->tab = $tab;
    }

    public function openYearModal(): void
    {
        $this->authorize('create', AcademicYear::class);
        $this->resetYearForm();
        $this->showYearModal = true;
    }

    public function openHolidayModal(): void
    {
        $this->authorize('create', AcademicYear::class);
        $this->resetHolidayForm();
        $activeYear = AcademicYear::where('is_active', true)->first();
        $this->holiday_academic_year_id = $activeYear?->id;
        $this->showHolidayModal = true;
    }

    public function openEventModal(): void
    {
        $this->authorize('create', AcademicYear::class);
        $this->resetEventForm();
        $activeYear = AcademicYear::where('is_active', true)->first();
        $this->event_academic_year_id = $activeYear?->id;
        $this->showEventModal = true;
    }

    public function saveYear(AcademicCalendarService $calendarService): void
    {
        $this->authorize('create', AcademicYear::class);

        $this->validate([
            'year_name' => ['required', 'string', 'max:255'],
            'year_start_date' => ['required', 'date'],
            'year_end_date' => ['required', 'date', 'after_or_equal:year_start_date'],
        ], [
            'year_name.required' => 'Nama Tahun Akademik wajib diisi.',
            'year_start_date.required' => 'Tanggal mulai wajib diisi.',
            'year_end_date.required' => 'Tanggal selesai wajib diisi.',
        ]);

        $calendarService->createAcademicYear([
            'name' => $this->year_name,
            'start_date' => $this->year_start_date,
            'end_date' => $this->year_end_date,
            'is_active' => $this->year_is_active,
        ]);

        session()->flash('toast', ['type' => 'success', 'message' => 'Tahun Akademik baru berhasil ditambahkan.']);
        $this->closeModal();
    }

    public function saveHoliday(AcademicCalendarService $calendarService): void
    {
        $this->authorize('create', AcademicYear::class);

        $this->validate([
            'holiday_academic_year_id' => ['nullable', 'exists:academic_years,id'],
            'holiday_name' => ['required', 'string', 'max:255'],
            'holiday_date' => ['required', 'date'],
            'holiday_type' => ['required', 'in:national,school'],
        ], [
            'holiday_name.required' => 'Nama hari libur wajib diisi.',
            'holiday_date.required' => 'Tanggal libur wajib diisi.',
        ]);

        $calendarService->createHoliday([
            'academic_year_id' => $this->holiday_academic_year_id,
            'name' => $this->holiday_name,
            'date' => $this->holiday_date,
            'type' => $this->holiday_type,
            'description' => $this->holiday_description !== '' ? $this->holiday_description : null,
        ]);

        session()->flash('toast', ['type' => 'success', 'message' => 'Hari libur baru berhasil ditambahkan.']);
        $this->closeModal();
    }

    public function saveEvent(AcademicCalendarService $calendarService): void
    {
        $this->authorize('create', AcademicYear::class);

        $this->validate([
            'event_academic_year_id' => ['nullable', 'exists:academic_years,id'],
            'event_title' => ['required', 'string', 'max:255'],
            'event_type' => ['required', 'in:ujian,pkl,mpls,khusus'],
            'event_start_date' => ['required', 'date'],
            'event_end_date' => ['required', 'date', 'after_or_equal:event_start_date'],
        ], [
            'event_title.required' => 'Judul kegiatan wajib diisi.',
            'event_start_date.required' => 'Tanggal mulai wajib diisi.',
            'event_end_date.required' => 'Tanggal selesai wajib diisi.',
        ]);

        $calendarService->createEvent([
            'academic_year_id' => $this->event_academic_year_id,
            'title' => $this->event_title,
            'type' => $this->event_type,
            'start_date' => $this->event_start_date,
            'end_date' => $this->event_end_date,
            'description' => $this->event_description !== '' ? $this->event_description : null,
        ]);

        session()->flash('toast', ['type' => 'success', 'message' => 'Kegiatan akademik berhasil ditambahkan.']);
        $this->closeModal();
    }

    public function deleteEvent(int $id, AcademicCalendarService $calendarService): void
    {
        $this->authorize('create', AcademicYear::class);
        $calendarService->deleteEvent($id);
        session()->flash('toast', ['type' => 'success', 'message' => 'Kegiatan akademik berhasil dihapus.']);
    }

    public function closeModal(): void
    {
        $this->showYearModal = false;
        $this->showHolidayModal = false;
        $this->showEventModal = false;
        $this->resetYearForm();
        $this->resetHolidayForm();
        $this->resetEventForm();
    }

    private function resetYearForm(): void
    {
        $this->year_name = '';
        $this->year_start_date = '';
        $this->year_end_date = '';
        $this->year_is_active = true;
        $this->resetValidation();
    }

    private function resetHolidayForm(): void
    {
        $this->holiday_academic_year_id = null;
        $this->holiday_name = '';
        $this->holiday_date = '';
        $this->holiday_type = 'national';
        $this->holiday_description = '';
        $this->resetValidation();
    }

    private function resetEventForm(): void
    {
        $this->event_academic_year_id = null;
        $this->event_title = '';
        $this->event_type = 'khusus';
        $this->event_start_date = '';
        $this->event_end_date = '';
        $this->event_description = '';
        $this->resetValidation();
    }

    public function render(AcademicCalendarService $calendarService)
    {
        $this->authorize('viewAny', AcademicYear::class);

        $academicYears = $calendarService->getAllAcademicYears();
        $holidays = $calendarService->getAllHolidays();
        $events = $calendarService->getAllEvents();

        return view('livewire.academic.academic-calendar-index', [
            'academicYears' => $academicYears,
            'holidays' => $holidays,
            'events' => $events,
        ])->layout('components.layouts.app');
    }
}
