import { Form, Head, Link } from '@inertiajs/react';
import { Bell, ChevronRight } from 'lucide-react';
import NotificationController from '@/actions/App/Http/Controllers/NotificationController';
import EmptyState from '@/components/member/empty-state';
import MemberPage from '@/components/member/member-page';
import Pagination from '@/components/member/pagination';
import { Button } from '@/components/ui/button';
import { formatDateTime } from '@/lib/format';
import { cn } from '@/lib/utils';
import { index, open } from '@/routes/notifications';
import type { Paginated } from '@/types';

type NotificationItem = {
    id: string;
    title: string;
    body: string;
    url: string | null;
    read: boolean;
    createdAt: string | null;
};

export default function Notifications({
    notifications,
}: {
    notifications: Paginated<NotificationItem>;
}) {
    const hasUnread = notifications.data.some((item) => !item.read);

    return (
        <>
            <Head title="Notifications" />

            <MemberPage
                title="Notifications"
                description="Updates about your account, payments and requests."
                actions={
                    hasUnread && (
                        <Form {...NotificationController.markAllRead.form()}>
                            {({ processing }) => (
                                <Button
                                    variant="outline"
                                    size="sm"
                                    disabled={processing}
                                >
                                    Mark all as read
                                </Button>
                            )}
                        </Form>
                    )
                }
            >
                {notifications.data.length === 0 ? (
                    <EmptyState
                        icon={Bell}
                        title="No notifications yet"
                        description="We'll let you know here when something happens on your account."
                    />
                ) : (
                    <ul className="divide-y divide-surface-100 overflow-hidden rounded-landing-lg border border-surface-200 bg-white">
                        {notifications.data.map((item) => (
                            <li key={item.id}>
                                <Link
                                    href={open(item.id)}
                                    className="flex items-start gap-4 px-4 py-4 transition-colors hover:bg-surface-50 sm:px-5"
                                >
                                    <span
                                        aria-hidden="true"
                                        className={cn(
                                            'mt-1.5 size-2 shrink-0 rounded-full',
                                            item.read
                                                ? 'bg-transparent'
                                                : 'bg-highlight',
                                        )}
                                    />
                                    <div className="min-w-0 flex-1">
                                        <p
                                            className={cn(
                                                'text-sm text-ink',
                                                !item.read && 'font-semibold',
                                            )}
                                        >
                                            {item.title}
                                            {!item.read && (
                                                <span className="sr-only">
                                                    {' '}
                                                    (unread)
                                                </span>
                                            )}
                                        </p>
                                        <p className="mt-0.5 text-sm text-ink-500">
                                            {item.body}
                                        </p>
                                        <p className="mt-1 text-xs text-ink-400">
                                            {formatDateTime(item.createdAt)}
                                        </p>
                                    </div>
                                    <ChevronRight className="mt-1 size-4 shrink-0 text-ink-300" />
                                </Link>
                            </li>
                        ))}
                    </ul>
                )}

                <Pagination page={notifications} />
            </MemberPage>
        </>
    );
}

Notifications.layout = {
    breadcrumbs: [{ title: 'Notifications', href: index() }],
};
