import AdminLayout from '@/Layouts/AdminLayout';
import { Head, Link, router } from '@inertiajs/react';
import { Calendar, CalendarCheck, ShoppingBag, TrendingUp, ArrowUpRight } from 'lucide-react';

interface Stats {
    upcoming_bookings: number;
    total_bookings: number;
    products: number;
    revenue: number;
}

interface RecentBooking {
    id: number;
    name: string;
    company: string | null;
    location: string | null;
    scheduled_at: string;
    status: string;
}

interface RecentOrder {
    id: string;
    customer: string;
    product: string;
    amount: string;
    date: string;
}

interface Props {
    stats: Stats;
    recentBookings: RecentBooking[];
    recentOrders: RecentOrder[];
}

export default function Dashboard({ stats, recentBookings, recentOrders }: Props) {
    const cards = [
        {
            name: 'UPCOMING BOOKINGS',
            value: stats.upcoming_bookings.toString(),
            sub: 'Next 30 days',
            icon: CalendarCheck,
        },
        {
            name: 'TOTAL BOOKINGS',
            value: stats.total_bookings.toString(),
            sub: 'All time',
            icon: Calendar,
        },
        {
            name: 'PRODUCTS',
            value: stats.products.toString(),
            sub: 'Live in store',
            icon: ShoppingBag,
        },
        {
            name: 'REVENUE',
            value: stats.revenue > 0 ? `₦${stats.revenue.toLocaleString()}` : '—',
            sub: 'NGN, all time',
            icon: TrendingUp,
        },
    ];

    return (
        <AdminLayout>
            <Head title="Overview" />

            {/* Page Header */}
            <div className="mb-6">
                <h1 className="font-display text-[1.625rem] font-normal tracking-[-0.02em] text-ink leading-none">
                    Overview
                </h1>
                <p className="mt-2.5 text-[0.9375rem] text-[#8C92AC]">
                    Snapshot of bookings, orders and store performance.
                </p>
            </div>

            {/* Stat Cards */}
            <div className="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-5">
                {cards.map((card) => (
                    <div
                        key={card.name}
                        className="bg-white rounded-xl border border-[#E8E6DF] p-5 flex flex-col gap-6"
                    >
                        <div className="flex items-center justify-between">
                            <span className="text-[10px] font-semibold tracking-[0.14em] uppercase text-[#8C92AC]">
                                {card.name}
                            </span>
                            <card.icon className="h-[15px] w-[15px] text-[#C0BDBA] flex-shrink-0" strokeWidth={1.5} />
                        </div>
                        <div>
                            <p className="font-display text-[2.25rem] font-normal text-ink leading-none">
                                {card.value}
                            </p>
                            <p className="mt-2 text-[11px] text-[#8C92AC]">{card.sub}</p>
                        </div>
                    </div>
                ))}
            </div>

            {/* Recent rows */}
            <div className="grid gap-4 lg:grid-cols-2">

                {/* Recent Bookings */}
                <div className="bg-white rounded-xl border border-[#E8E6DF] overflow-hidden">
                    <div className="flex items-center justify-between px-6 py-5 border-b border-[#F0EDE8]">
                        <h2 className="font-display text-[1.1rem] font-normal text-ink">Recent bookings</h2>
                        <Link
                            href="/admin/bookings"
                            className="flex items-center gap-1 text-[11px] text-[#8C92AC] hover:text-ink transition-colors"
                        >
                            View all <ArrowUpRight className="h-3 w-3" strokeWidth={2} />
                        </Link>
                    </div>

                    {recentBookings.length > 0 ? recentBookings.map((b) => (
                        <div
                            key={b.id}
                            className="flex items-center justify-between px-6 py-5 border-b border-[#F0EDE8] last:border-0 hover:bg-[#FAFAF8] transition-colors"
                        >
                            <div>
                                <p className="text-sm font-semibold text-ink">{b.name}</p>
                                <p className="mt-0.5 text-[11px] text-[#8C92AC]">
                                    {b.company ?? '—'}{b.location ? ` · ${b.location}` : ''}
                                </p>
                            </div>
                            <div className="text-right">
                                <p className="text-[12px] font-semibold text-ink">
                                    {new Date(b.scheduled_at).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })}
                                </p>
                                <p className="mt-0.5 text-[10px] text-[#8C92AC] uppercase tracking-wide">
                                    {new Date(b.scheduled_at).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} WAT
                                </p>
                            </div>
                        </div>
                    )) : (
                        <div className="px-6 py-10 text-center text-[13px] text-[#8C92AC]">
                            No bookings yet. They'll appear here once submitted.
                        </div>
                    )}
                </div>

                {/* Recent Orders */}
                <div className="bg-white rounded-xl border border-[#E8E6DF] overflow-hidden">
                    <div className="flex items-center justify-between px-6 py-5 border-b border-[#F0EDE8]">
                        <h2 className="font-display text-[1.1rem] font-normal text-ink">Recent orders</h2>
                        <Link
                            href="/admin/orders"
                            className="flex items-center gap-1 text-[11px] text-[#8C92AC] hover:text-ink transition-colors"
                        >
                            View all <ArrowUpRight className="h-3 w-3" strokeWidth={2} />
                        </Link>
                    </div>

                    {recentOrders.length > 0 ? recentOrders.map((o) => (
                        <div
                            key={o.id}
                            className="flex items-center justify-between px-6 py-5 border-b border-[#F0EDE8] last:border-0 hover:bg-[#FAFAF8] transition-colors"
                        >
                            <div>
                                <p className="text-sm font-semibold text-ink">{o.customer}</p>
                                <p className="mt-0.5 text-[11px] text-[#8C92AC]">{o.product}</p>
                            </div>
                            <div className="text-right">
                                <p className="text-[12px] font-semibold text-ink">{o.amount}</p>
                                <p className="mt-0.5 text-[11px] text-primary/70">{o.date}</p>
                            </div>
                        </div>
                    )) : (
                        <div className="px-6 py-10 text-center text-[13px] text-[#8C92AC]">
                            Orders will appear here once the store is connected.
                        </div>
                    )}
                </div>
            </div>
        </AdminLayout>
    );
}
