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 { Button } from '@/components/ui/button';
import { CirclePlus, Puzzle, SquarePen } from 'lucide-react';
import { ImageIconBox } from '@/components/core/Icon';
import FacilityMaker from './FacilityMaker';
import { DeleteAlertButton } from '@/components/core/DeleteAlertButton';
import { useState } from 'react';
import { FacilityType } from '@/types/dashboard/facility';

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



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


interface PageProps {
    facilities: {
        data: FacilityType[],
        links: PaginationLinkType[],
        path: string,
        current_page: number
        last_page: number
        per_page: number
        total: number
    };
}

export default function Index({ facilities }: PageProps) {

    const [openMaker, setOpenMaker] = useState(false)
    const [facility, setFacility] = useState<FacilityType | null>(null)

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

    const meta = { current_page, last_page, per_page, total, path }


    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Facilities" />
            <IndexPageLayout title='Facilities' path='/dashboard/facilities' headerButtons={<FacilityMaker facility={facility} setFacility={setFacility} open={openMaker} setOpen={setOpenMaker} />}>
                <>
                    <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"> <Puzzle className="w-10 h-10 text-primary" /> </div>

                                    <div className="space-y-1">
                                        <h3 className="text-lg font-semibold">No Room Facilities Yet</h3>
                                        <p className="text-sm text-muted-foreground max-w-sm">
                                            Add facilities like WiFi, air conditioning, or a minibar to make your rooms more comfortable and appealing to guests.
                                        </p>
                                    </div>

                                    <Button onClick={() => setOpenMaker(true)} className="bg-primary hover:bg-primary hover:text-white text-white" > <CirclePlus className="h-4 w-4" />  Add First Facility </Button>
                                </div>
                                :
                                <>
                                    <Table>
                                        <TableHeader>
                                            <TableRow >
                                                <TableHead className='w-8'>#</TableHead>
                                                <TableHead>Icon</TableHead>
                                                <TableHead>Label</TableHead>
                                                <TableHead>Scope</TableHead>
                                                <TableHead>Published</TableHead>
                                                <TableHead >Updated At</TableHead>
                                                <TableHead >Created At</TableHead>
                                                <TableHead className="text-right">Action</TableHead>
                                            </TableRow>
                                        </TableHeader>
                                        <TableBody>
                                            {data.map((item) => (
                                                <TableRow key={item.id}>
                                                    <TableCell  >{item.id}</TableCell>
                                                    <TableCell ><ImageIconBox is_icon={item.is_icon} value={item.icon_value} /></TableCell>
                                                    <TableCell >{item.name}</TableCell>
                                                    <TableCell >{item.scope}</TableCell>
                                                    <TableCell ><Badge variant={item.is_published ? 'default' : 'outline'}>{item.is_published ? ' published' : 'draft'}</Badge></TableCell>

                                                    <TableCell >{new Date(item.updated_at).toLocaleDateString()}</TableCell>
                                                    <TableCell >{new Date(item.created_at).toLocaleDateString()}</TableCell>
                                                    <TableCell className=" flex gap-2 justify-end items-center">
                                                        <Button variant='outline' size='icon-sm' onClick={() => { setFacility(item), setOpenMaker(true) }}><SquarePen /></Button>
                                                        <DeleteAlertButton deleteId={item.id} model='facilities' title='Facility' />
                                                    </TableCell>
                                                </TableRow>
                                            ))}
                                        </TableBody>
                                    </Table>
                                    <TablePagination links={links} meta={meta} />
                                </>
                            }
                        </CardContent>
                        <CardFooter>
                        </CardFooter>
                    </Card>
                </>
            </IndexPageLayout>
        </AppLayout >
    );
}
