import { Head, Link, usePage } from '@inertiajs/react';
import { FileText, Plus } from 'lucide-react';
import { useMemo, useState } from 'react';
import {
    create as customerOrdersCreate,
    index as customerOrdersIndex,
} from '@/actions/App/Http/Controllers/CustomerOrderController';
import { CustomerOrderSheetDialog } from '@/components/customer-orders/customer-order-sheet-dialog';
import { CustomerOrdersDataTable } from '@/components/customer-orders/customer-orders-data-table';
import { Button } from '@/components/ui/button';
import {
    Card,
    CardContent,
    CardDescription,
    CardHeader,
    CardTitle,
} from '@/components/ui/card';
import { useLanguage } from '@/contexts/language-context';
import AppLayout from '@/layouts/app-layout';
import { normalizeInertiaList } from '@/lib/table-defaults';
import { dashboard } from '@/routes';

export default function CustomerOrdersIndex() {
    const { t, locale } = useLanguage();
    const p = t.customerOrdersPage;
    const page = usePage();
    const auth = page.props.auth ?? {};
    const { orders, customers = [] } = page.props;
    const orderRows = useMemo(() => normalizeInertiaList(orders), [orders]);
    const customerRows = useMemo(() => normalizeInertiaList(customers), [customers]);
    const [sheetOpen, setSheetOpen] = useState(false);

    const breadcrumbs = useMemo(
        () => [
            { title: t.dashboard.breadcrumb, href: dashboard() },
            { title: t.nav.customers, href: '#' },
            { title: p.breadcrumb, href: customerOrdersIndex.url() },
        ],
        [t.dashboard.breadcrumb, t.nav.customers, p.breadcrumb],
    );

    const tableLabels = useMemo(() => ({ ...p }), [p]);

    const sheetLabels = useMemo(
        () => ({
            sheetTitle: p.sheetTitle,
            sheetDescription: p.sheetDescription,
            startDate: p.sheetStartDate,
            endDate: p.sheetEndDate,
            customerLabel: p.customerLabel,
            customerOptionalPlaceholder: p.sheetCustomerPlaceholder,
            customerOptionalHint: p.sheetCustomerHint,
            customersSelectedCount: p.sheetCustomersSelectedCount,
            allCustomers: p.sheetAllCustomers,
            ordersLabel: p.sheetOrdersLabel,
            ordersOptionalPlaceholder: p.sheetOrdersPlaceholder,
            ordersOptionalHint: p.sheetOrdersHint,
            ordersNeedDates: p.sheetOrdersNeedDates,
            ordersNeedDatesHint: p.sheetOrdersNeedDatesHint,
            ordersSelectedCount: p.sheetOrdersSelectedCount,
            ordersEmpty: p.sheetOrdersEmpty,
            allOrders: p.sheetAllOrders,
            noCustomersHint: p.noCustomersHint,
            generate: p.sheetGenerate,
            cancel: p.sheetCancel,
            required: t.validation?.required ?? 'Required',
            dateRangeInvalid: p.sheetDateRangeInvalid,
        }),
        [p, t.validation],
    );

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title={p.headTitle} />
            <CustomerOrderSheetDialog
                open={sheetOpen}
                onOpenChange={setSheetOpen}
                customers={customerRows}
                orders={orderRows}
                labels={sheetLabels}
                locale={locale}
            />
            <div className="flex h-full flex-1 flex-col gap-6 overflow-x-auto p-4">
                <Card>
                    <CardHeader className="flex flex-col gap-4 sm:flex-row sm:items-start sm:justify-between">
                        <div className="space-y-2">
                            <CardTitle>{p.headTitle}</CardTitle>
                            <CardDescription>{p.listSubtitle}</CardDescription>
                        </div>
                        <div className="flex flex-wrap gap-2">
                            {auth.canViewCustomerOrders === true ? (
                                <Button type="button" variant="outline" onClick={() => setSheetOpen(true)}>
                                    <FileText className="size-4" />
                                    {p.sheetButton}
                                </Button>
                            ) : null}
                            {auth.canCreateCustomerOrders === true ? (
                                <Button asChild>
                                    <Link href={customerOrdersCreate.url()} prefetch>
                                        <Plus className="size-4" />
                                        {p.createOrder}
                                    </Link>
                                </Button>
                            ) : null}
                        </div>
                    </CardHeader>
                    <CardContent>
                        <CustomerOrdersDataTable
                            orders={orderRows}
                            labels={tableLabels}
                            locale={locale}
                        />
                    </CardContent>
                </Card>
            </div>
        </AppLayout>
    );
}
