import { Head, usePage } from '@inertiajs/react';
import { DashboardChartCard } from '@/features/dashboard/components/dashboard-chart-panel';
import { DashboardStatWidget } from '@/features/dashboard/components/dashboard-stat-widget';
import type { DashboardPayload } from '@/features/dashboard/types';
import { useI18n } from '@/hooks/use-i18n';
import type { Auth } from '@/types';

export default function AdminDashboard({ dashboard }: { dashboard: DashboardPayload }) {
    const { t, locale } = useI18n();
    const { auth } = usePage<{ auth: Auth }>().props;

    const formatDateTick = (iso: string) =>
        new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric' }).format(
            new Date(`${iso}T12:00:00`),
        );

    const formatStatusLabel = (status: string) =>
        t(`dashboard.charts.account_status.${status}`);

    return (
        <>
            <Head title={t('dashboard.page.head_admin')} />

            <section className="space-y-8 mt-4">
                <div className="relative overflow-hidden rounded-2xl border border-border/60 bg-gradient-to-br from-primary/12 via-card to-card p-6 shadow-sm md:p-8">
                    <div className="pointer-events-none absolute -right-20 -top-20 h-64 w-64 rounded-full bg-primary/20 blur-3xl" />
                    <div className="relative space-y-2">
                        <p className="text-xs font-medium uppercase tracking-widest text-primary">
                            ContaFlow
                        </p>
                        <h1 className="text-2xl font-semibold tracking-tight md:text-3xl">
                            {t(dashboard.title_key)}
                        </h1>
                        <p className="max-w-2xl text-sm text-muted-foreground md:text-base">
                            {t(dashboard.subtitle_key)}
                        </p>
                        {auth?.user?.name ? (
                            <p className="text-sm text-muted-foreground">{auth.user.name}</p>
                        ) : null}
                    </div>
                </div>

                <div className="grid gap-4 sm:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-6">
                    {dashboard.widgets.map((w) => (
                        <DashboardStatWidget
                            key={w.key}
                            label={t(w.label_key)}
                            value={w.value}
                            icon={w.icon}
                            hint={w.hint_key ? t(w.hint_key) : undefined}
                        />
                    ))}
                </div>

                <div className="grid gap-4 md:grid-cols-2">
                    {dashboard.charts.map((chart) => (
                        <DashboardChartCard
                            key={chart.id}
                            chart={chart}
                            title={t(chart.title_key)}
                            subtitle={chart.subtitle_key ? t(chart.subtitle_key) : undefined}
                            formatXLabel={formatDateTick}
                            formatStatusLabel={formatStatusLabel}
                        />
                    ))}
                </div>
            </section>
        </>
    );
}
