import Card from '@/Components/Card';
import { Badge } from '@/Components/ui/badge';
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import { capitalize } from '@/lib/utils';
import { Alert, User } from '@/types';
import { Head, usePage } from '@inertiajs/react';
import dayjs from 'dayjs';
import { BellRing } from 'lucide-react';

interface PatientListProps {
    alerts: Alert[];
    patient: User;
}

export default function PatientList({ alerts, patient }: PatientListProps) {
    const user = usePage().props.auth.user;
    const alertCards = alerts.map((alert) => {
        const lastNotification = alert.notifications.length
            ? alert.notifications[0]
            : null;
        const lastNotificationDate = lastNotification
            ? dayjs(lastNotification.created_at).format('DD.MM.YYYY - HH:mm')
            : null;
        return (
            <Card key={alert.id} className="relative p-6 text-black">
                <div className="font-bold">
                    {capitalize(alert.condition_as_text)}
                </div>
                <div className="mt-2 text-zinc-500">{alert.message}</div>
                <div className="mt-4 flex flex-row justify-end">
                    {lastNotificationDate && (
                        <Badge>
                            <BellRing className="mr-2 h-4 w-4 p-0" />
                            <span>{lastNotificationDate}</span>
                        </Badge>
                    )}
                </div>
            </Card>
        );
    });

    const EmptyMessage = () => {
        return <div className="text-center">Nessun avviso assegnato.</div>;
    };

    const backLinkLabel =
        patient.id === user.id
            ? 'Dashboard'
            : `${patient.name} ${patient.surname}`;

    return (
        <AuthenticatedLayout
            backLink={route('dashboard.personal', { target: patient.id })}
            backLinkLabel={backLinkLabel}
        >
            <Head title="Avvisi" />

            <div className="py-2">
                <div className="mx-auto max-w-7xl sm:px-6 lg:px-8">
                    <div className="overflow-hidden sm:rounded-lg">
                        <div className="p-6 text-gray-900">
                            <div className="scroll-m-20 text-4xl font-light tracking-tight lg:text-5xl">
                                Avvisi
                            </div>
                            <div className="container mx-auto flex flex-col items-center py-10">
                                <div className="flex max-w-[700px] flex-col gap-4">
                                    {alertCards.length ? (
                                        alertCards
                                    ) : (
                                        <EmptyMessage />
                                    )}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
