import { AvailabilityCalendar } from '@admin/components/dashboard/availability-calendar';
import { DashboardPanel } from '@admin/components/dashboard/dashboard-panel';
import { DashboardTablePanel } from '@admin/components/dashboard/dashboard-table-panel';
import { EarningsChart } from '@admin/components/dashboard/earnings-chart';
import { StatCard } from '@admin/components/dashboard/stat-card';
import DashboardLayout from '@admin/layouts/dashboard-layout';
import type { DashboardPageProps } from '@admin/types/dashboard';
import { Head, Link } from '@inertiajs/react';
import type { ReactElement } from 'react';

function Dashboard({
    summary,
    availability,
    earnings,
    bookingStatuses,
    site,
    recentBookings,
    recentMessages,
}: DashboardPageProps) {
    return (
        <>
            <Head title="Dashboard">
                <link rel="stylesheet" href="/admin/assets/libs/apexcharts/apexcharts.css" />
            </Head>

            <div className="grid grid-cols-12 items-stretch gap-5">
                {summary.map((card) => (
                    <StatCard key={card.key} card={card} />
                ))}

                <div className="col-span-12">
                    <DashboardPanel
                        title="Room availability"
                        description="Each date is the night of that day. A stay holds the room from check-in until the morning of check-out."
                        actionHref={route('admin.bookings')}
                        actionLabel="Bookings"
                    >
                        <AvailabilityCalendar availability={availability} />
                    </DashboardPanel>
                </div>

                <div className="col-span-12 xl:col-span-8">
                    <DashboardPanel
                        title="Earnings"
                        description={`${earnings.thisMonth} this month · ${earnings.lastMonth} last month. Confirmed, in-house, and completed stays, counted on the arrival month.`}
                    >
                        <EarningsChart earnings={earnings} />
                    </DashboardPanel>
                </div>

                <div className="col-span-12 xl:col-span-4">
                    <DashboardPanel title="Bookings" description="Every stay on record, by status">
                        <ul className="mb-0 space-y-3">
                            {bookingStatuses.map((status) => (
                                <li key={status.label} className="flex items-center justify-between gap-3">
                                    <span className="text-sm text-gray-600 dark:text-white/70">{status.label}</span>
                                    <span className="font-semibold">{status.count}</span>
                                </li>
                            ))}
                        </ul>
                    </DashboardPanel>
                </div>

                <div className="col-span-12">
                    <DashboardPanel title="On the website" description="Published records guests can see">
                        <div className="grid grid-cols-2 gap-3 sm:grid-cols-4 xl:grid-cols-8">
                            {site.map((item) => (
                                <Link key={item.label} href={item.href} className="rounded-sm border border-gray-100 px-3 py-3 dark:border-white/10">
                                    <span className="block text-xs text-gray-500 dark:text-white/60">{item.label}</span>
                                    <span className="block text-xl font-semibold">{item.count}</span>
                                </Link>
                            ))}
                        </div>
                    </DashboardPanel>
                </div>

                <div className="col-span-12 lg:col-span-7">
                    <DashboardTablePanel title="Recent bookings" actionHref={route('admin.bookings')} actionLabel="View all">
                        <table className="ti-custom-table ti-custom-table-head mb-0 w-full">
                            <thead>
                                <tr>
                                    <th scope="col" className="!text-start">
                                        Guest
                                    </th>
                                    <th scope="col" className="!text-start">
                                        Room
                                    </th>
                                    <th scope="col" className="!text-start">
                                        Dates
                                    </th>
                                    <th scope="col" className="!text-start">
                                        Total
                                    </th>
                                    <th scope="col" className="!text-start">
                                        Status
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                {recentBookings.length === 0 ? (
                                    <tr>
                                        <td colSpan={5} className="py-8 text-center text-gray-500 dark:text-white/70">
                                            No bookings yet.
                                        </td>
                                    </tr>
                                ) : (
                                    recentBookings.map((booking) => (
                                        <tr key={booking.id}>
                                            <td className="font-medium">{booking.guest}</td>
                                            <td>
                                                <span className="block">{booking.room}</span>
                                                <span className="text-xs text-gray-500">{booking.number}</span>
                                            </td>
                                            <td className="whitespace-nowrap text-xs">{booking.dates}</td>
                                            <td className="whitespace-nowrap text-xs">{booking.total}</td>
                                            <td>
                                                <span className="badge bg-primary/10 text-primary">{booking.statusLabel}</span>
                                            </td>
                                        </tr>
                                    ))
                                )}
                            </tbody>
                        </table>
                    </DashboardTablePanel>
                </div>

                <div className="col-span-12 lg:col-span-5">
                    <DashboardTablePanel
                        title="Recent contact messages"
                        actionHref={route('admin.contact-messages')}
                        actionLabel="View all"
                    >
                        <table className="ti-custom-table ti-custom-table-head mb-0 w-full">
                            <thead>
                                <tr>
                                    <th scope="col" className="!text-start">
                                        From
                                    </th>
                                    <th scope="col" className="!text-start">
                                        Subject
                                    </th>
                                    <th scope="col" className="!text-start">
                                        Status
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                {recentMessages.length === 0 ? (
                                    <tr>
                                        <td colSpan={3} className="py-8 text-center text-gray-500 dark:text-white/70">
                                            No messages yet.
                                        </td>
                                    </tr>
                                ) : (
                                    recentMessages.map((message) => (
                                        <tr key={message.id}>
                                            <td>
                                                <span className="block font-medium">{message.name}</span>
                                                <span className="text-xs text-gray-500">{message.email}</span>
                                            </td>
                                            <td className="max-w-0 truncate">{message.subject}</td>
                                            <td>
                                                <span
                                                    className={`badge ${message.is_read ? 'bg-secondary/10 text-secondary' : 'bg-warning/10 text-warning'}`}
                                                >
                                                    {message.is_read ? 'Read' : 'Unread'}
                                                </span>
                                            </td>
                                        </tr>
                                    ))
                                )}
                            </tbody>
                        </table>
                    </DashboardTablePanel>
                </div>
            </div>
        </>
    );
}

function DashboardPageLayout({ greeting, children }: DashboardPageProps & { children: ReactElement }) {
    return (
        <DashboardLayout title="Dashboard" subtitle="Home" description={`Welcome back, ${greeting.name}`}>
            {children}
        </DashboardLayout>
    );
}

Dashboard.layout = DashboardPageLayout;

export default Dashboard;
