import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import DrugPrescriptionDeleteAction from '@/Pages/DrugPrescription/DrugPrescriptionDeleteAction';
import { DrugPrescriptionsTable } from '@/Pages/DrugPrescription/DrugPrescriptionsTable';
import { DrugPrescription, DrugPrescriptionsResponse, User } from '@/types';
import { Head } from '@inertiajs/react';
import { ColumnDef } from '@tanstack/react-table';

interface AdminListProps {
    drugPrescriptions: DrugPrescriptionsResponse;
    availableTimingTypes: string[];
}

export default function AdminList({
    drugPrescriptions,
    availableTimingTypes,
}: AdminListProps) {
    const tableColumns: ColumnDef<DrugPrescription>[] = [
        {
            header: 'Utente',
            cell: ({ row }) => {
                const drugPrescription: DrugPrescription = row.original;
                const user: User = drugPrescription.user;

                return <span>{`${user.name} ${user.surname}`}</span>;
            },
        },
        {
            header: 'Farmaco',
            cell: ({ row }) => {
                const drugPrescription: DrugPrescription = row.original;
                const drug = drugPrescription.drug;

                return (
                    <span>{`${drug.name} (${drug.active_ingredient})`}</span>
                );
            },
        },
        {
            header: 'Quantità',
            cell: ({ row }) => {
                const drugPrescription: DrugPrescription = row.original;
                const drug = drugPrescription.drug;

                return (
                    <span>{`${drugPrescription.quantity} ${drug.package_unit}`}</span>
                );
            },
        },
        {
            id: 'actions',
            cell: ({ row }) => {
                const drugPrescription: DrugPrescription = row.original;

                return (
                    <DrugPrescriptionDeleteAction
                        drugPrescription={drugPrescription}
                    />
                );
            },
        },
    ];

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

            <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">
                                Promemoria Farmaci
                            </div>
                            <div className="container mx-auto py-10">
                                <DrugPrescriptionsTable
                                    columns={tableColumns}
                                    availableTimingTypes={availableTimingTypes}
                                    data={drugPrescriptions.data}
                                    rowCount={drugPrescriptions.total}
                                    pageSize={drugPrescriptions.per_page}
                                    pageCount={drugPrescriptions.last_page}
                                />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </AuthenticatedLayout>
    );
}
