import { Head } from '@inertiajs/react';
import AppLayout from '@/layouts/app-layout';
import type { BreadcrumbItem } from '@/types';
import IndexPageLayout from '@/components/core/IndexPageLayout';
import { Table, TableBody, TableCell, 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 { CirclePlus, Utensils } from 'lucide-react';
import { ImageIconBox } from '@/components/core/Icon';
import LinkButton, { ViewButton, EditButton } from '@/components/core/LinkButton';
import { DeleteButton } from '@/components/core/DeleteButton';
import { PaginationLink } from '@/types/dashboard';
import { MealPackageType } from '@/types/dashboard/meal-package';
import { SystemCurrencyType } from '@/types/dashboard/currency';

const breadcrumbs: BreadcrumbItem[] = [
    {
        title: 'Meal Packages',
        href: '/dashboard/meal-packages',
    },
];


interface MealPackagesPageProps {
    meal_packages: {
        data: MealPackageType[],
        links: PaginationLink[],
        path: string,
        current_page: number
        last_page: number
        per_page: number
        total: number
    };
    system_currency: SystemCurrencyType
}

export default function Index({ meal_packages, system_currency }: MealPackagesPageProps) {

    const { data, links, current_page, last_page, per_page, total, path } = meal_packages
    const meta = { current_page, last_page, per_page, total, path }

    console.log(meal_packages);


    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Meal Packages" />
            <IndexPageLayout title='Meal Packages' add path='/dashboard/meal-packages' description='Manage all meal packages in one place. View, organize, and control options like breakfast, half-board, or full-board, including pricing and availability, for seamless use in room bookings.' >
                <>

                    <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">
                                        <Utensils className="w-10 h-10 text-primary" />
                                    </div>

                                    <div className="space-y-1">
                                        <h3 className="text-lg font-semibold">No Meal Packages Yet</h3>
                                        <p className="text-sm text-muted-foreground max-w-sm"> Create your first meal package to enhance your guests’ stay with curated dining options. </p>
                                    </div>

                                    <LinkButton className='bg-primary hover:bg-primary hover:text-white text-white' href='/dashboard/meal-packages/create' ><CirclePlus className=" h-4 w-4" /> Add First Package </LinkButton>
                                </div>
                            ) : (
                                <>
                                    <Table>
                                        <TableHeader>
                                            <TableRow >
                                                <TableHead className='w-8'>#</TableHead>
                                                <TableHead className='w-8'>Sort</TableHead>
                                                <TableHead>Icon</TableHead>
                                                <TableHead>Meal Package</TableHead>
                                                <TableHead >Scope</TableHead>
                                                <TableHead >Tag</TableHead>
                                                <TableHead >Price</TableHead>
                                                <TableHead >Published</TableHead>
                                                <TableHead >Default</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 >{item.sort_order || '- '}</TableCell>
                                                    <TableCell ><ImageIconBox is_icon={item.is_icon} value={item.icon_value} /></TableCell>
                                                    <TableCell >{item.title}</TableCell>
                                                    <TableCell >{item.scope}</TableCell>
                                                    <TableCell >{item.tag ? <Badge variant={'outline'}>{item.tag}</Badge> : '-'}</TableCell>
                                                    <TableCell >{item.is_variant ? <Badge variant={'outline'}>variant</Badge> : <>{system_currency} {Number(item.price).toLocaleString()}</>}</TableCell>
                                                    <TableCell ><Badge variant={item.is_published ? 'default' : 'outline'}>{item.is_published ? ' published' : 'draft'}</Badge></TableCell>
                                                    <TableCell ><Badge variant={item.default_availability ? 'default' : 'outline'}>{item.default_availability ? 'active' : 'block'}</Badge></TableCell>
                                                    <TableCell >{new Date(item?.updated_at || '').toLocaleDateString()}</TableCell>
                                                    <TableCell className=" flex gap-2 justify-end items-center">
                                                        <ViewButton path='/dashboard/meal-packages' id={item.id} />
                                                        <EditButton path='/dashboard/meal-packages' id={item.id} />
                                                        <DeleteButton deleteId={item.id} model='meal-packages' title='Meal-Package' />
                                                    </TableCell>
                                                </TableRow>
                                            ))}
                                        </TableBody>
                                    </Table>
                                    <TablePagination links={links} meta={meta} />
                                </>
                            )}
                        </CardContent>
                        <CardFooter>
                        </CardFooter>
                    </Card>
                </>
            </IndexPageLayout>
        </AppLayout >
    );
}
