import { Link, usePage } from '@inertiajs/react';
import { useState } from 'react';
import Dropdown from '../Components/Dropdown';

const menuItems = [
    {
        label: 'Dashboard',
        href: '/dashboard',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <rect x="3" y="3" width="7" height="7" rx="1" />
                <rect x="14" y="3" width="7" height="7" rx="1" />
                <rect x="3" y="14" width="7" height="7" rx="1" />
                <rect x="14" y="14" width="7" height="7" rx="1" />
            </svg>
        ),
    },
    {
        label: 'Trips',
        href: '/trips',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <path d="M3 10.5 12 4l9 6.5" />
                <path d="M5 9.5V20h14V9.5" />
                <path d="M9 20v-6h6v6" />
            </svg>
        ),
    },
    {
        label: 'Categories',
        href: '/categories',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <path d="M20 12 12 20l-8-8V4h8l8 8Z" />
                <circle cx="8" cy="8" r="1.5" />
            </svg>
        ),
    },
    {
        label: 'Customers',
        href: '/customers',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <circle cx="9" cy="8" r="3" />
                <path d="M3 20a6 6 0 0 1 12 0" />
                <path d="M17 11a3 3 0 0 0 0-6" />
                <path d="M21 20a5 5 0 0 0-4-4.9" />
            </svg>
        ),
    },
    {
        label: 'Purchases',
        href: '/purchases',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <path d="M6 2h12l1 5H5l1-5Z" />
                <path d="M5 7h14v13H5z" />
                <path d="M9 11h6" />
                <path d="M9 15h4" />
            </svg>
        ),
    },
    {
        label: 'Invoices',
        href: '/invoices',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <path d="M6 2h12v20l-6-3-6 3V2Z" />
                <path d="M9 7h6" />
                <path d="M9 11h6" />
                <path d="M9 15h4" />
            </svg>
        ),
    },
    {
        label: 'Payments',
        href: '/payments',
        icon: (
            <svg viewBox="0 0 24 24" fill="none" stroke="currentColor">
                <rect x="2.5" y="5" width="19" height="14" rx="2" />
                <path d="M2.5 10h19" />
                <path d="M7 15h3" />
            </svg>
        ),
    },
];

export default function AppLayout({ children }) {
    const { url, props } = usePage();

    const user = props.auth?.user;

    const [mobileMenuOpen, setMobileMenuOpen] = useState(false);

    const isActive = (href) => {
        return (
            url === href ||
            url.startsWith(`${href}/`)
        );
    };

    return (
        <div className="min-h-screen bg-gray-50">

            <div className="flex min-h-screen">

                {/* =====================================================
                    SIDEBAR
                ====================================================== */}
                <aside className="hidden w-64 shrink-0 border-r border-gray-200 bg-white md:flex md:flex-col">

                    {/* Logo */}
                    <div className="flex h-20 items-center border-b border-gray-100 px-6">
                        <Link
                            href="/dashboard"
                            className="flex items-center gap-3"
                        >
                            <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-gray-900 text-white shadow-sm">
                                <svg
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    strokeWidth="1.8"
                                    className="h-5 w-5"
                                >
                                    <path d="M3 10.5 12 4l9 6.5" />
                                    <path d="M5 9.5V20h14V9.5" />
                                    <path d="M9 20v-6h6v6" />
                                </svg>
                            </div>

                            <div>
                                <p className="text-base font-bold tracking-tight text-gray-900">
                                    Jastip
                                </p>

                                <p className="text-xs text-gray-400">
                                    Management
                                </p>
                            </div>
                        </Link>
                    </div>

                    {/* Navigation */}
                    <div className="flex-1 overflow-y-auto px-4 py-6">

                        <p className="mb-3 px-3 text-[10px] font-bold uppercase tracking-[0.15em] text-gray-400">
                            Main Menu
                        </p>

                        <nav className="space-y-1">
                            {menuItems.map((item) => {
                                const active = isActive(item.href);

                                return (
                                    <Link
                                        key={item.href}
                                        href={item.href}
                                        className={`
                                            group flex items-center gap-3 rounded-xl px-3.5 py-3 text-sm font-medium transition-all duration-150
                                            ${
                                                active
                                                    ? 'bg-gray-900 text-white shadow-sm'
                                                    : 'text-gray-500 hover:bg-gray-100 hover:text-gray-900'
                                            }
                                        `}
                                    >
                                        <span
                                            className={`
                                                flex h-8 w-8 items-center justify-center rounded-lg transition
                                                ${
                                                    active
                                                        ? 'bg-white/10 text-white'
                                                        : 'text-gray-400 group-hover:text-gray-700'
                                                }
                                            `}
                                        >
                                            <span className="h-[18px] w-[18px]">
                                                {item.icon}
                                            </span>
                                        </span>

                                        <span className="flex-1">
                                            {item.label}
                                        </span>

                                        {active && (
                                            <span className="h-1.5 w-1.5 rounded-full bg-white" />
                                        )}
                                    </Link>
                                );
                            })}
                        </nav>
                    </div>

                    {/* Sidebar Bottom */}
                    <div className="border-t border-gray-100 p-4">
                        <div className="rounded-xl bg-gray-50 p-4">
                            <p className="text-xs font-semibold text-gray-700">
                                Jastip System
                            </p>

                            <p className="mt-1 text-[11px] leading-5 text-gray-400">
                                Manage your trips, purchases and payments in
                                one place.
                            </p>
                        </div>
                    </div>
                </aside>

                {/* =====================================================
                    MAIN
                ====================================================== */}
                <div className="flex min-w-0 flex-1 flex-col">

                    {/* =================================================
                        TOPBAR
                    ================================================== */}
                    <header className="sticky top-0 z-30 flex h-20 items-center justify-between border-b border-gray-200 bg-white/95 px-5 backdrop-blur md:px-8">

                        {/* Mobile Header */}
                        <div className="flex items-center gap-3 md:hidden">

                            {/* Hamburger */}
                            <button
                                type="button"
                                onClick={() => setMobileMenuOpen(true)}
                                className="flex h-10 w-10 items-center justify-center rounded-xl border border-gray-200 bg-white text-gray-700 shadow-sm transition hover:bg-gray-50"
                                aria-label="Open menu"
                            >
                                <svg
                                    viewBox="0 0 24 24"
                                    fill="none"
                                    stroke="currentColor"
                                    strokeWidth="1.8"
                                    className="h-5 w-5"
                                >
                                    <path d="M4 6h16" />
                                    <path d="M4 12h16" />
                                    <path d="M4 18h16" />
                                </svg>
                            </button>

                            {/* Brand */}
                            <Link
                                href="/dashboard"
                                className="flex items-center gap-3"
                            >
                                <div className="flex h-9 w-9 items-center justify-center rounded-lg bg-gray-900 text-white">
                                    <svg
                                        viewBox="0 0 24 24"
                                        fill="none"
                                        stroke="currentColor"
                                        strokeWidth="1.8"
                                        className="h-5 w-5"
                                    >
                                        <path d="M3 10.5 12 4l9 6.5" />
                                        <path d="M5 9.5V20h14V9.5" />
                                        <path d="M9 20v-6h6v6" />
                                    </svg>
                                </div>

                                <div>
                                    <p className="text-sm font-bold text-gray-900">
                                        Jastip
                                    </p>

                                    <p className="text-[10px] text-gray-400">
                                        Management
                                    </p>
                                </div>
                            </Link>
                        </div>
                        
                        {/* Desktop Breadcrumb / Title */}
                        <div className="hidden md:block">
                            <p className="text-xs font-medium text-gray-400">
                                Jastip Management
                            </p>

                            <p className="mt-0.5 text-sm font-semibold text-gray-900">
                                Business Dashboard
                            </p>
                        </div>

                        {/* Right */}
                        <div className="ml-auto flex items-center gap-3">

                            {/* Status */}
                            <div className="hidden items-center gap-2 rounded-full border border-gray-200 bg-gray-50 px-3 py-2 sm:flex">
                                <span className="h-2 w-2 rounded-full bg-green-500" />

                                <span className="text-xs font-medium text-gray-500">
                                    System Online
                                </span>
                            </div>

                            {/* User */}
                            {user && (
                                <Dropdown>
                                    <Dropdown.Trigger>
                                        <button
                                            type="button"
                                            className="group flex items-center gap-3 rounded-xl border border-gray-200 bg-white px-2.5 py-2 transition hover:border-gray-300 hover:bg-gray-50 focus:outline-none"
                                        >
                                            {/* Avatar */}
                                            <div className="flex h-9 w-9 items-center justify-center rounded-lg bg-gray-900 text-sm font-bold text-white">
                                                {user.name
                                                    ?.charAt(0)
                                                    .toUpperCase()}
                                            </div>

                                            {/* Name */}
                                            <div className="hidden text-left sm:block">
                                                <p className="max-w-[140px] truncate text-sm font-semibold text-gray-800">
                                                    {user.name}
                                                </p>

                                                <p className="text-[10px] text-gray-400">
                                                    Administrator
                                                </p>
                                            </div>

                                            {/* Chevron */}
                                            <svg
                                                className="hidden h-4 w-4 text-gray-400 transition group-hover:text-gray-700 sm:block"
                                                xmlns="http://www.w3.org/2000/svg"
                                                viewBox="0 0 20 20"
                                                fill="currentColor"
                                            >
                                                <path
                                                    fillRule="evenodd"
                                                    d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414 1 1 0 011.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z"
                                                    clipRule="evenodd"
                                                />
                                            </svg>
                                        </button>
                                    </Dropdown.Trigger>

                                    <Dropdown.Content>
                                        <Dropdown.Link
                                            href={route('profile.edit')}
                                        >
                                            Profile
                                        </Dropdown.Link>

                                        <Dropdown.Link
                                            href={route('logout')}
                                            method="post"
                                            as="button"
                                        >
                                            Log Out
                                        </Dropdown.Link>
                                    </Dropdown.Content>
                                </Dropdown>
                            )}
                        </div>
                    </header>

                    {/* =================================================
                        PAGE CONTENT
                    ================================================== */}
                    <main className="min-w-0 flex-1">
                        {children}
                    </main>
                </div>
            </div>

            {/* =====================================================
                MOBILE SIDEBAR
            ====================================================== */}

            {mobileMenuOpen && (
                <div
                    className="fixed inset-0 z-40 bg-gray-900/40 backdrop-blur-sm md:hidden"
                    onClick={() => setMobileMenuOpen(false)}
                />
            )}

            <aside
                className={`
                    fixed inset-y-0 left-0 z-50 w-72 bg-white shadow-2xl
                    transition-transform duration-300 ease-in-out md:hidden
                    ${
                        mobileMenuOpen
                            ? 'translate-x-0'
                            : '-translate-x-full'
                    }
                `}
            >
                {/* Mobile Logo */}
                <div className="flex h-20 items-center justify-between border-b border-gray-100 px-5">
                    <Link
                        href="/dashboard"
                        onClick={() => setMobileMenuOpen(false)}
                        className="flex items-center gap-3"
                    >
                        <div className="flex h-10 w-10 items-center justify-center rounded-xl bg-gray-900 text-white shadow-sm">
                            <svg
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                strokeWidth="1.8"
                                className="h-5 w-5"
                            >
                                <path d="M3 10.5 12 4l9 6.5" />
                                <path d="M5 9.5V20h14V9.5" />
                                <path d="M9 20v-6h6v6" />
                            </svg>
                        </div>

                        <div>
                            <p className="text-base font-bold tracking-tight text-gray-900">
                                Jastip
                            </p>

                            <p className="text-xs text-gray-400">
                                Management
                            </p>
                        </div>
                    </Link>

                    {/* Close Button */}
                    <button
                        type="button"
                        onClick={() => setMobileMenuOpen(false)}
                        className="flex h-9 w-9 items-center justify-center rounded-lg text-gray-400 transition hover:bg-gray-100 hover:text-gray-700"
                        aria-label="Close menu"
                    >
                        <svg
                            viewBox="0 0 24 24"
                            fill="none"
                            stroke="currentColor"
                            strokeWidth="2"
                            className="h-5 w-5"
                        >
                            <path d="M6 6l12 12" />
                            <path d="M18 6 6 18" />
                        </svg>
                    </button>
                </div>

                {/* Mobile Navigation */}
                <div className="flex h-[calc(100vh-5rem)] flex-col overflow-y-auto px-4 py-6">

                    <p className="mb-3 px-3 text-[10px] font-bold uppercase tracking-[0.15em] text-gray-400">
                        Main Menu
                    </p>

                    <nav className="space-y-1">
                        {menuItems.map((item) => {
                            const active = isActive(item.href);

                            return (
                                <Link
                                    key={item.href}
                                    href={item.href}
                                    onClick={() => setMobileMenuOpen(false)}
                                    className={`
                                        group flex items-center gap-3 rounded-xl
                                        px-3.5 py-3 text-sm font-medium
                                        transition-all duration-150
                                        ${
                                            active
                                                ? 'bg-gray-900 text-white shadow-sm'
                                                : 'text-gray-500 hover:bg-gray-100 hover:text-gray-900'
                                        }
                                    `}
                                >
                                    <span
                                        className={`
                                            flex h-8 w-8 items-center justify-center
                                            rounded-lg transition
                                            ${
                                                active
                                                    ? 'bg-white/10 text-white'
                                                    : 'text-gray-400 group-hover:text-gray-700'
                                            }
                                        `}
                                    >
                                        <span className="h-[18px] w-[18px]">
                                            {item.icon}
                                        </span>
                                    </span>

                                    <span className="flex-1">
                                        {item.label}
                                    </span>

                                    {active && (
                                        <span className="h-1.5 w-1.5 rounded-full bg-white" />
                                    )}
                                </Link>
                            );
                        })}
                    </nav>

                    {/* Bottom Info */}
                    <div className="mt-auto border-t border-gray-100 pt-5">
                        <div className="rounded-xl bg-gray-50 p-4">
                            <p className="text-xs font-semibold text-gray-700">
                                Jastip System
                            </p>

                            <p className="mt-1 text-[11px] leading-5 text-gray-400">
                                Manage your trips, purchases and payments in
                                one place.
                            </p>
                        </div>
                    </div>
                </div>
            </aside>            
        </div>
    );
}