import { Head, Link } from '@inertiajs/react';
import { Plus, Trash2 } from 'lucide-react';
import { DataTable } from '@/components/data-table';

import { DeleteConfirmation } from '@/components/modal/delete-confirmation';
import {
    AlertDialogMedia,
} from "@/components/ui/alert-dialog"
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import { formatDate } from '@/lib/formatters';
import purchaseRoutes from '@/routes/purchase';
import type { Purchase } from '@/types';
import { usePurchaseIndex } from './hooks/use-purchase-index';
import { getColumns } from './partials/columns';

export default function PurchaseIndex({ purchases }: { purchases: Purchase[] }) {
    const {
        isDeleteDialogOpen,
        isBulkDeleteDialogOpen,
        purchaseToDelete,
        rowsToDelete,
        rowSelection,
        setRowSelection,
        setRowsToDelete,
        setIsDeleteDialogOpen,
        setIsBulkDeleteDialogOpen,
        onDelete,
        confirmDelete,
        confirmBulkDelete,
    } = usePurchaseIndex();

    const columns = getColumns({ onDelete });

    return (
        <div className="flex flex-col gap-6 p-6">
            <Head title="Belanja" />

            <div className="flex items-center justify-between">
                <div>
                    <h1 className="text-3xl font-bold tracking-tight">Belanja</h1>
                </div>
                <Link href={purchaseRoutes.create().url}>
                    <Button>
                        <Plus className="size-4" />
                        Tambah
                    </Button>
                </Link>
            </div>

            <Card className="overflow-hidden border-none shadow-lg bg-card/50 backdrop-blur-sm p-5">
                <CardContent className="p-0">
                    <DataTable
                        columns={columns}
                        data={purchases}
                        rowSelection={rowSelection}
                        onRowSelectionChange={setRowSelection}
                        bulkActions={[
                            {
                                label: 'Hapus Terpilih',
                                onClick: (rows) => {
                                    setRowsToDelete(rows);
                                    setIsBulkDeleteDialogOpen(true);
                                },
                                icon: Trash2,
                                variant: 'destructive'
                            },
                        ]}
                    />
                </CardContent>
            </Card>

            {/* Single Delete Confirmation */}
            <DeleteConfirmation
                isOpen={isDeleteDialogOpen}
                onOpenChange={setIsDeleteDialogOpen}
                title="Hapus belanja?"
                description={
                    <>
                        Tindakan ini tidak dapat dibatalkan. Belanja <strong>{purchaseToDelete?.purchase_date
                            ? formatDate(purchaseToDelete?.purchase_date)
                            : purchaseToDelete?.note || '-'}</strong> akan dihapus secara permanen.
                    </>
                }
                onDelete={confirmDelete}
            />

            {/* Bulk Delete Confirmation */}
            <DeleteConfirmation
                isOpen={isBulkDeleteDialogOpen}
                onOpenChange={setIsBulkDeleteDialogOpen}
                title={`Hapus ${rowsToDelete.length} belanja?`}
                description={
                    <>
                        Tindakan ini tidak dapat dibatalkan. <strong>{rowsToDelete.length}</strong> item yang terpilih akan dihapus secara permanen.
                    </>
                }
                onDelete={confirmBulkDelete}
            />
        </div>
    );
}

PurchaseIndex.layout = {
    breadcrumbs: [
        {
            title: 'Kelola',
        },
    ],
};
