import { Head, Link } from '@inertiajs/react';
import { Trash2, Plus } from 'lucide-react';
import { DataTable } from '@/components/data-table';
import { DeleteConfirmation } from '@/components/modal/delete-confirmation';
import { Button } from '@/components/ui/button';
import { Card, CardContent } from '@/components/ui/card';
import roleRoutes from '@/routes/system/role';
import type { Role } from '@/types';
import type { PaginatedData } from '@/types/pagination';
import { useRoleIndex } from './hooks/use-role-index';
import { getColumns } from './partials/columns';
import { usePermission } from '@/hooks/use-permission';

export default function RoleIndex({ roles }: { roles: PaginatedData<Role> }) {
    const { hasPermission } = usePermission();
    const {
        isDeleteDialogOpen,
        isBulkDeleteDialogOpen,
        roleToDelete,
        rowsToDelete,
        rowSelection,
        setRowSelection,
        setRowsToDelete,
        setIsDeleteDialogOpen,
        setIsBulkDeleteDialogOpen,
        onDelete,
        confirmDelete,
        confirmBulkDelete,
    } = useRoleIndex();

    const columns = getColumns({ onDelete }).filter(
        (col) => col.id !== 'actions' || hasPermission('Edit:Role') || hasPermission('Delete:Role')
    );

    return (
        <div className="flex flex-col gap-6 p-6">
            <Head title="Role & Izin Akses" />

            <div className="flex items-center justify-between">
                <div>
                    <h1 className="text-3xl font-bold tracking-tight">Role & Izin Akses</h1>
                </div>
                {hasPermission('Create:Role') && (
                    <Link href={roleRoutes.create().url}>
                        <Button className="gap-2">
                            <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={roles.data}
                        meta={roles}
                        showSelection={hasPermission('DeleteAny:Role')}
                        rowSelection={rowSelection}
                        onRowSelectionChange={setRowSelection}
                        bulkActions={
                            hasPermission('DeleteAny:Role')
                                ? [
                                      {
                                          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 role?"
                description={
                    <>
                        Tindakan ini tidak dapat dibatalkan. Role <strong>{roleToDelete?.name}</strong> akan dihapus secara permanen.
                    </>
                }
                onDelete={confirmDelete}
            />

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

RoleIndex.layout = {
    breadcrumbs: [
        {
            title: 'Sistem',
        },
    ],
};
