import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import ContactDeleteAction from '@/Pages/Contact/ContactDeleteAction';
import { ContactsTable } from '@/Pages/Contact/ContactsTable';
import { Contact, ContactsResponse, User } from '@/types';
import { Head } from '@inertiajs/react';
import { ColumnDef } from '@tanstack/react-table';

interface AdminListProps {
    contacts: ContactsResponse;
    availableNotificationChannels: string[];
}

export default function AdminList({
    contacts,
    availableNotificationChannels,
}: AdminListProps) {
    const tableColumns: ColumnDef<Contact>[] = [
        {
            header: 'Utente',
            cell: ({ row }) => {
                const contact: Contact = row.original;
                const user: User = contact.user;

                return <span>{`${user.name} ${user.surname}`}</span>;
            },
        },
        {
            accessorKey: 'label',
            header: 'Etichetta',
        },
        {
            accessorKey: 'email',
            header: 'Email',
        },
        {
            id: 'actions',
            cell: ({ row }) => {
                const contact: Contact = row.original;

                return <ContactDeleteAction contact={contact} />;
            },
        },
    ];

    return (
        <AuthenticatedLayout
            backLink={route('dashboard')}
            backLinkLabel="Dashboard"
        >
            <Head title="Contatti" />

            <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">
                                Contatti
                            </div>
                            <div className="container mx-auto py-10">
                                <ContactsTable
                                    columns={tableColumns}
                                    availableNotificationChannels={
                                        availableNotificationChannels
                                    }
                                    data={contacts.data}
                                    rowCount={contacts.total}
                                    pageSize={contacts.per_page}
                                    pageCount={contacts.last_page}
                                />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
