import { Head } from '@inertiajs/react';
import Heading from '@/components/heading';
import {
    Card,
    CardContent,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';
import { dashboard } from '@/routes';
import { expenses } from '@/routes/admin/dashboards';
import {
    DashboardExportButtons,
    DashboardFiltersBar,
    type DashboardFilters,
    type FilterOptions,
} from './dashboard-filters';
import { KpiCard, LineChart, StackedCategoryChart } from './dashboard-widgets';

export default function DashboardsExpenses({
    overview,
    filters,
    filterOptions,
    can,
}: {
    overview: {
        kpis: {
            total: string;
            achats: string;
            salaires: string;
            frais_generaux: string;
            average_monthly: string;
        };
        by_type: Array<{ type: string; label: string; amount: string }>;
        trend: Array<{
            key: string;
            label: string;
            achats: string;
            salaires: string;
            frais_generaux: string;
            total: string;
        }>;
    };
    filters: DashboardFilters;
    filterOptions: FilterOptions;
    can: { export: boolean };
}) {
    return (
        <>
            <Head title="Évolution des Dépenses" />
            <div className="flex h-full flex-1 flex-col gap-6 p-4 md:p-6">
                <div className="flex flex-col gap-4 lg:flex-row lg:items-center lg:justify-between">
                    <Heading
                        title="Évolution des Dépenses"
                        description="Répartition achats, salaires et frais généraux par période"
                    />
                    <DashboardExportButtons view="expenses" filters={filters} canExport={can.export} />
                </div>

                <DashboardFiltersBar
                    active="expenses"
                    filters={filters}
                    filterOptions={filterOptions}
                    showCollaboratorFilter
                />

                <div className="grid gap-4 sm:grid-cols-2 xl:grid-cols-5">
                    <KpiCard label="Total dépenses" value={overview.kpis.total} hint="Achats + NDF + salaires" />
                    <KpiCard label="Achats" value={overview.kpis.achats} hint="Hors immobilisations" />
                    <KpiCard label="Salaires" value={overview.kpis.salaires} />
                    <KpiCard label="Frais généraux" value={overview.kpis.frais_generaux} hint="Notes de frais" />
                    <KpiCard label="Moyenne mensuelle" value={overview.kpis.average_monthly} hint="Total ÷ mois de la période" />
                </div>

                <div className="grid gap-6 lg:grid-cols-2">
                    <Card>
                        <CardHeader>
                            <CardTitle>Évolution par catégorie</CardTitle>
                            <CardDescription>Courbes achats, salaires et frais généraux</CardDescription>
                        </CardHeader>
                        <CardContent>
                            <LineChart
                                items={overview.trend}
                                exportTitle="Évolution par catégorie"
                                series={[
                                    { key: 'achats', label: 'Achats', color: '#2563eb' },
                                    { key: 'salaires', label: 'Salaires', color: '#059669' },
                                    { key: 'frais_generaux', label: 'Frais généraux', color: '#d97706' },
                                ]}
                            />
                        </CardContent>
                    </Card>

                    <Card>
                        <CardHeader>
                            <CardTitle>Répartition globale</CardTitle>
                            <CardDescription>Ventilation par type de dépense</CardDescription>
                        </CardHeader>
                        <CardContent>
                            <StackedCategoryChart
                                items={overview.by_type}
                                exportTitle="Répartition globale"
                            />
                        </CardContent>
                    </Card>
                </div>
            </div>
        </>
    );
}

DashboardsExpenses.layout = {
    breadcrumbs: [
        { title: 'Tableau de bord', href: dashboard() },
        { title: 'Évolution des Dépenses', href: expenses() },
    ],
};
