import { Head, Link, router } from '@inertiajs/react';
import { ChevronLeft, ChevronRight, List } from 'lucide-react';
import { useState } from 'react';
import LeaveRequestController from '@/actions/App/Http/Controllers/Admin/LeaveRequestController';
import { CalendarGrid } from './calendar-grid';
import type { Absence } from './calendar-grid';
import { LeaveRequestFormModal } from './leave-request-form-modal';
import type {
    BalanceOption,
    EmployeeOption,
    LeaveTypeOption,
} from './leave-request-form';
import Heading from '@/components/heading';
import { Button } from '@/components/ui/button';
import { dashboard } from '@/routes';
import { calendar, index } from '@/routes/admin/leave-requests';

const MONTH_LABELS = [
    'Janvier',
    'Février',
    'Mars',
    'Avril',
    'Mai',
    'Juin',
    'Juillet',
    'Août',
    'Septembre',
    'Octobre',
    'Novembre',
    'Décembre',
];

function navigate(month: number, year: number) {
    let nextMonth = month;
    let nextYear = year;

    if (nextMonth < 1) {
        nextMonth = 12;
        nextYear -= 1;
    } else if (nextMonth > 12) {
        nextMonth = 1;
        nextYear += 1;
    }

    router.get(
        calendar.url(),
        { month: nextMonth, year: nextYear },
        {
            preserveScroll: true,
        },
    );
}

function goToToday() {
    const today = new Date();

    router.get(
        calendar.url(),
        { month: today.getMonth() + 1, year: today.getFullYear() },
        { preserveScroll: true },
    );
}

export default function LeaveCalendar({
    absences,
    month,
    year,
    canViewAll,
    canCreate,
    leaveTypes,
    createEmployees,
    createBalances,
    createDefaultEmployeeId,
}: {
    absences: Absence[];
    month: number;
    year: number;
    canViewAll: boolean;
    canCreate: boolean;
    leaveTypes: LeaveTypeOption[];
    createEmployees: EmployeeOption[];
    createBalances: BalanceOption[];
    createDefaultEmployeeId: number | null;
}) {
    const [modalOpen, setModalOpen] = useState(false);
    const [selectedDate, setSelectedDate] = useState<string | null>(null);

    function handleDayClick(isoDate: string) {
        setSelectedDate(isoDate);
        setModalOpen(true);
    }

    return (
        <>
            <Head title="Calendrier des absences" />

            <div className="flex h-full flex-1 flex-col gap-6 p-4 md:p-6">
                <div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
                    <Heading
                        title="Calendrier des absences"
                        description={
                            canViewAll
                                ? "Vue d'ensemble de toutes les équipes"
                                : 'Vue de votre équipe'
                        }
                    />
                </div>

                <div className="flex items-center justify-center gap-4">
                    <Button
                        variant="outline"
                        size="icon"
                        onClick={() => navigate(month - 1, year)}
                    >
                        <ChevronLeft />
                    </Button>
                    <span className="min-w-40 text-center font-medium">
                        {MONTH_LABELS[month - 1]} {year}
                    </span>
                    <Button
                        variant="outline"
                        size="icon"
                        onClick={() => navigate(month + 1, year)}
                    >
                        <ChevronRight />
                    </Button>
                    <Button variant="outline" onClick={goToToday}>
                        Aujourd'hui
                    </Button>
                </div>

                <div className="flex items-center gap-4 text-sm">
                    <span className="flex items-center gap-1.5">
                        <span className="size-3 rounded bg-emerald-100 dark:bg-emerald-900/40" />
                        Approuvée
                    </span>
                    <span className="flex items-center gap-1.5">
                        <span className="size-3 rounded border border-dashed border-amber-400" />
                        En attente de validation
                    </span>
                </div>

                <CalendarGrid
                    month={month}
                    year={year}
                    absences={absences}
                    canCreate={canCreate}
                    onDayClick={handleDayClick}
                />
            </div>

            {canCreate && (
                <LeaveRequestFormModal
                    open={modalOpen}
                    onOpenChange={setModalOpen}
                    employees={createEmployees}
                    leaveTypes={leaveTypes}
                    initialBalances={createBalances}
                    defaultEmployeeId={createDefaultEmployeeId}
                    defaultStartDate={selectedDate}
                    submitUrl={`${LeaveRequestController.store.url()}?redirect=calendar&month=${month}&year=${year}`}
                />
            )}
        </>
    );
}

LeaveCalendar.layout = {
    breadcrumbs: [
        { title: 'Tableau de bord', href: dashboard() },
        { title: 'Congés & absences', href: index() },
        { title: 'Calendrier', href: calendar() },
    ],
};
