import { Head, Link, router, usePage } from '@inertiajs/react';
import { PlaceholderPattern } from '@/components/ui/placeholder-pattern';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';
import IndexPageLayout from '@/components/core/IndexPageLayout';
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from '@/components/ui/table';
import { Card, CardContent, CardFooter } from '@/components/ui/card';
import { TablePagination } from '@/components/core/TablePagination';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { CirclePlus, Eye, Gift, Pencil, SquarePen, Trash } from 'lucide-react';
import { IconBox, ImageIconBox } from '@/components/core/Icon';
import LinkButton, { ViewButton, EditButton } from '@/components/core/LinkButton';
import { DeleteButton } from '@/components/core/DeleteButton';

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Add-Ons',
        href: '/dashboard/add-ons',
    },
];

type ItemProp = {
    id: number,
    title: string,
    description: string,
    price: number,
    scope: string,
    currency: string,
    tag: string,
    icon: string,
    icon_url: string,
    is_published: boolean,
    default_active: boolean,
    updated_at: string
};

type PaginationLinkType = {
    url: string | null
    label: string
    active: boolean
}


interface ItemPageProps {
    addons: {
        data: ItemProp[],
        links: PaginationLinkType[],
        path: string,
        current_page: number
        last_page: number
        per_page: number
        total: number
    };
}

export default function Index() {

    const { addons } = usePage().props as unknown as ItemPageProps;
    const { data, links, current_page, last_page, per_page, total, path } = addons
    const meta = { current_page, last_page, per_page, total, path }

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Add-Ons" />
            <IndexPageLayout title='Add-Ons' add path='/dashboard/add-ons' >
                <>

                    <Card className='py-4 rounded-sm shadow-none gap-2'>
                        <CardContent className='px-4'>
                            {data.length === 0 ? (

                                <div className="flex flex-col items-center justify-center py-16 text-center space-y-4">
                                    <div className="p-4 rounded-full bg-muted">
                                        <Gift className="w-10 h-10 text-primary" />
                                    </div>

                                    <div className="space-y-1">
                                        <h3 className="text-lg font-semibold">No Add-Ons Yet</h3>
                                        <p className="text-sm text-muted-foreground max-w-sm">Create add-ons to offer personalized extras and increase booking value.</p>
                                    </div>

                                    <LinkButton className='bg-primary hover:bg-primary hover:text-white text-white' href='/dashboard/add-ons/create' ><CirclePlus className=" h-4 w-4" /> Add First Add-On </LinkButton>
                                </div>
                            ) : (
                                <>
                                    <Table>
                                        <TableHeader>
                                            <TableRow >
                                                <TableHead className='w-8'>#</TableHead>
                                                <TableHead>Icon</TableHead>
                                                <TableHead>Add-On</TableHead>
                                                <TableHead>Scope</TableHead>
                                                <TableHead >Publish</TableHead>
                                                <TableHead >Active</TableHead>
                                                <TableHead >Tag</TableHead>
                                                <TableHead >Price</TableHead>
                                                <TableHead >Updated At</TableHead>
                                                <TableHead className="text-right">Action</TableHead>
                                            </TableRow>
                                        </TableHeader>
                                        <TableBody>
                                            {data.map((item) => (
                                                <TableRow key={item.id}>
                                                    <TableCell >{item.id}</TableCell>
                                                    <TableCell ><ImageIconBox icon={item.icon} iconUrl={item.icon_url} /></TableCell>
                                                    <TableCell >{item.title}</TableCell>
                                                    <TableCell >{item.scope}</TableCell>
                                                    <TableCell ><Badge variant={item.is_published ? 'default' : 'outline'}>{item.is_published ? ' published' : 'draft'}</Badge></TableCell>
                                                    <TableCell ><Badge variant={item.default_active ? 'default' : 'outline'}>{item.default_active ? ' active' : 'block'}</Badge></TableCell>
                                                    <TableCell >{item.tag ? <Badge variant={'outline'}>{item.tag}</Badge> : '-'}</TableCell>
                                                    <TableCell >{item.currency} {Number(item.price).toPrecision()}</TableCell>
                                                    <TableCell >{new Date(item.updated_at).toLocaleDateString()}</TableCell>
                                                    <TableCell className=" flex gap-2 justify-end items-center">
                                                        <ViewButton path='/dashboard/add-ons' id={item.id} />
                                                        <EditButton path='/dashboard/add-ons' id={item.id} />
                                                        <DeleteButton deleteId={item.id} model='add-ons' title='Add-Ons' />
                                                    </TableCell>
                                                </TableRow>
                                            ))}
                                        </TableBody>
                                    </Table>
                                    <TablePagination links={links} meta={meta} />
                                </>
                            )}
                        </CardContent>
                        <CardFooter>
                        </CardFooter>
                    </Card>
                </>
            </IndexPageLayout>
        </AppLayout >
    );
}
