import { Head, usePage } from '@inertiajs/react';
import {
    Card,
    CardContent,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';
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 { ActivityLogItem, Auth } from '@/types';

export default function ClientDashboard({
    dashboard,
    activity,
}: {
    dashboard: DashboardPayload;
    activity: {
        subject: ActivityLogItem[];
    };
}) {
    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_client')} />

            <section className="space-y-8">
                <div className="relative overflow-hidden rounded-2xl border border-border/60 bg-gradient-to-br from-primary/10 via-card to-muted/30 p-6 shadow-sm md:p-8">
                    <div className="pointer-events-none absolute -left-16 bottom-0 h-48 w-48 rounded-full bg-primary/15 blur-3xl" />
                    <div className="relative space-y-2">
                        <h1 className="text-2xl font-semibold tracking-tight md:text-3xl">
                            {t(dashboard.title_key)}
                            {auth?.user?.name ? (
                                <span className="text-muted-foreground">, {auth.user.name}</span>
                            ) : null}
                        </h1>
                        <p className="max-w-2xl text-sm text-muted-foreground md:text-base">
                            {t(dashboard.subtitle_key)}
                        </p>
                    </div>
                </div>

                <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
                    {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 lg: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>

                <Card className="border-border/80">
                    <CardHeader>
                        <CardTitle>{t('dashboard.activity.client_subject_title')}</CardTitle>
                        <CardDescription>
                            {t('dashboard.activity.client_subject_description')}
                        </CardDescription>
                    </CardHeader>
                    <CardContent className="space-y-2 text-sm">
                        {activity.subject.length === 0 && (
                            <p className="text-muted-foreground">
                                {t('dashboard.activity.empty')}
                            </p>
                        )}

                        {activity.subject.map((item) => (
                            <div
                                key={item.id}
                                className="rounded-lg border border-border/70 bg-muted/20 p-3"
                            >
                                <p className="font-medium">{item.description}</p>
                                <p className="mt-1 text-xs text-muted-foreground">
                                    {t('dashboard.activity.log_prefix')}: {item.log_name} ·{' '}
                                    {t('dashboard.activity.at_prefix')}{' '}
                                    {new Date(item.created_at).toLocaleString(locale)}
                                </p>
                            </div>
                        ))}
                    </CardContent>
                </Card>
            </section>
        </>
    );
}
