import { Head } from '@inertiajs/react';
import { Alert, AlertDescription, AlertTitle } from '@/components/ui/alert';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';

const breadcrumbs: BreadcrumbItem[] = [
    { title: 'Paramètres', href: '/settings' },
    { title: 'Grille tarifaire', href: '/settings/grille-tarifaire' },
];

type ZonePrice = {
    zone_name: string;
    city: string;
    commune: string;
    fee: number;
};

type PricingGridProps = {
    zones: ZonePrice[];
    tableMissing?: boolean;
};

const formatCurrency = (value: number) =>
    new Intl.NumberFormat('fr-FR', {
        style: 'currency',
        currency: 'XOF',
        maximumFractionDigits: 0,
    }).format(value);

export default function PricingGridSettings({ zones, tableMissing }: PricingGridProps) {
    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Grille tarifaire" />

            <div className="space-y-8 rounded-3xl bg-gradient-to-b from-blue-50 via-white to-emerald-50 p-4 md:p-6 dark:from-blue-950/40 dark:via-neutral-950 dark:to-emerald-950/40">
                <div>
                    <h1 className="text-2xl font-semibold text-blue-700 dark:text-blue-200">
                        Grille tarifaire
                    </h1>
                    <p className="text-sm text-muted-foreground">
                        Liste des tarifs par zone définis dans les paramètres.
                    </p>
                </div>

                {tableMissing && (
                    <Alert variant="destructive">
                        <AlertTitle>Erreur</AlertTitle>
                        <AlertDescription>
                            La table des zones est introuvable. Lancez les migrations
                            avant d’afficher la grille tarifaire.
                        </AlertDescription>
                    </Alert>
                )}

                {!tableMissing && zones.length === 0 ? (
                    <div className="rounded-2xl border border-blue-100/80 bg-white/90 p-6 text-sm text-muted-foreground shadow-sm dark:border-blue-900/50 dark:bg-neutral-900/80">
                        Aucune zone définie pour le moment.
                    </div>
                ) : null}

                {!tableMissing && zones.length > 0 ? (
                    <div className="rounded-2xl border border-blue-100/80 bg-white/90 p-4 shadow-lg dark:border-blue-900/50 dark:bg-neutral-900/80">
                        <div className="overflow-x-auto">
                            <table className="w-full text-sm">
                                <thead className="border-b border-blue-100/80 text-left text-muted-foreground dark:border-blue-900/50">
                                    <tr>
                                        <th className="px-3 py-2 font-medium">Zone</th>
                                        <th className="px-3 py-2 font-medium">Ville</th>
                                        <th className="px-3 py-2 font-medium">
                                            Arrondissement
                                        </th>
                                        <th className="px-3 py-2 text-right font-medium">
                                            Tarif
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {zones.map((zone, index) => (
                                        <tr
                                            key={`${zone.zone_name}-${index}`}
                                            className="border-b border-blue-50/80 last:border-b-0 dark:border-blue-900/30"
                                        >
                                            <td className="px-3 py-3 font-medium">
                                                {zone.zone_name}
                                            </td>
                                            <td className="px-3 py-3">{zone.city}</td>
                                            <td className="px-3 py-3">{zone.commune}</td>
                                            <td className="px-3 py-3 text-right font-semibold text-emerald-700 dark:text-emerald-300">
                                                {formatCurrency(zone.fee)}
                                            </td>
                                        </tr>
                                    ))}
                                </tbody>
                            </table>
                        </div>
                    </div>
                ) : null}
            </div>
        </AppLayout>
    );
}
