import { Head, Link, router } from '@inertiajs/react';
import { Plus } from 'lucide-react';
import { useState } from 'react';
import { CardTable } from '@/components/card-table';
import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog';
import { useCardTableExpand } from '@/components/hooks/use-card-table-expand';
import { PageHeader } from '@/components/page-header';
import { Button } from '@/components/ui/button';
import { useServerTable } from '@/hooks/use-server-table';
import {
    destroy,
    create as cuttingCreate,
    index as cuttingIndex,
    edit as cuttingEdit,
} from '@/routes/admin/manage/cuttings';
import type { Cutting } from './columns';
import { CuttingCardRow } from './cutting-card';
import { CuttingItemSubRow } from './cutting-sub-row';

type Props = {
    cuttings: {
        data: Cutting[];
        current_page: number;
        last_page: number;
        per_page: number;
        total: number;
    };
};

export default function CuttingIndex({ cuttings }: Props) {
    const [deleting, setDeleting] = useState<Cutting | null>(null);
    const expand = useCardTableExpand(true);

    const pagination = {
        current_page: cuttings.current_page,
        last_page: cuttings.last_page,
        per_page: cuttings.per_page,
        total: cuttings.total,
    };

    const {
        search,
        handlePageChange,
        handlePerPageChange,
        handleSearchChange,
    } = useServerTable({
        route: () => cuttingIndex.url(),
        pagination,
    });

    function handleDelete() {
        if (!deleting) {
            return;
        }

        router.delete(destroy.url(deleting.id), {
            onSuccess: () => setDeleting(null),
        });
    }

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

            <div className="flex h-full flex-1 flex-col gap-6 overflow-x-auto p-4 md:p-6">
                <PageHeader
                    title="Cutting"
                    actions={
                        <Button asChild>
                            <Link href={cuttingCreate.url()}>
                                <Plus className="h-4 w-4" />
                                Tambah
                            </Link>
                        </Button>
                    }
                />

                <CardTable
                    data={cuttings.data}
                    getItemKey={(c) => c.id}
                    expandedKeys={expand.expandedKeys}
                    onToggleExpand={expand.toggleExpand}
                    searchValue={search}
                    onSearchChange={handleSearchChange}
                    searchPlaceholder="Cari berdasarkan nama produk..."
                    pagination={pagination}
                    onPageChange={handlePageChange}
                    onPerPageChange={handlePerPageChange}
                    renderCard={({
                        item,
                        index,
                        isExpanded,
                        onToggleExpand,
                    }) => (
                        <CuttingCardRow
                            cutting={item}
                            index={
                                (pagination.current_page - 1) *
                                    pagination.per_page +
                                index +
                                1
                            }
                            isExpanded={isExpanded}
                            onToggleExpand={onToggleExpand}
                            onEdit={(c) => {
                                router.visit(cuttingEdit.url(c.id));
                            }}
                            onDelete={(c) => setDeleting(c)}
                        />
                    )}
                    renderSubContent={(cutting) => (
                        <CuttingItemSubRow cutting={cutting} />
                    )}
                />

                <DeleteConfirmDialog
                    target={deleting}
                    onOpenChange={(open) => {
                        if (!open) {
                            setDeleting(null);
                        }
                    }}
                    title="Hapus Cutting"
                    description={(cutting) =>
                        `Apakah Anda yakin ingin menghapus cutting "${cutting.cutting_results?.[0]?.product_name ?? '-'}"? Tindakan ini tidak dapat dibatalkan.`
                    }
                    onConfirm={handleDelete}
                />
            </div>
        </>
    );
}
