"use client";

import ReactLenis from "lenis/react";
import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider";
import NavbarStyleFullscreen from '@/components/navbar/NavbarStyleFullscreen/NavbarStyleFullscreen';
import FooterSimple from '@/components/sections/footer/FooterSimple';
import ProductCatalog from "@/components/ecommerce/productCatalog/ProductCatalog";
import { useProductCatalog } from "@/hooks/useProductCatalog";
import { useState } from "react";
import ProductCart from "@/components/ecommerce/cart/ProductCart";
import { useCart } from "@/hooks/useCart";
import { useCheckout } from "@/hooks/useCheckout";

export default function ShopPage() {
    const {
        products,
        isLoading,
        search,
        setSearch,
        filters,
    } = useProductCatalog({ basePath: "/shop" });

    const {
        items: cartItems,
        isOpen: cartOpen,
        setIsOpen: setCartOpen,
        updateQuantity,
        removeItem,
        total: cartTotal,
        getCheckoutItems,
    } = useCart();

    const { checkout, isLoading: isCheckoutLoading } = useCheckout();

    const handleCheckout = async () => {
        if (cartItems.length === 0) return;

        const currentUrl = new URL(window.location.href);
        currentUrl.searchParams.set("success", "true");

        await checkout(getCheckoutItems(), { successUrl: currentUrl.toString() });
    };

    if (isLoading) {
        return (
            <ThemeProvider
                defaultButtonVariant="slide-background"
                defaultTextAnimation="entrance-slide"
                borderRadius="pill"
                contentWidth="mediumSmall"
                sizing="mediumLarge"
                background="circleGradient"
                cardStyle="glass-elevated"
                primaryButtonStyle="diagonal-gradient"
                secondaryButtonStyle="layered"
                headingFontWeight="normal"
            >
                <ReactLenis root>
                    <div id="navbar" data-section="navbar">
                        <NavbarStyleFullscreen
                            brandName="Prestige Hotels"
                            navItems={[
                                { name: "Home", id: "/" },
                                { name: "Rooms", id: "rooms" },
                                { name: "Amenities", id: "amenities" },
                                { name: "About", id: "about" },
                                { name: "Contact", id: "contact" },
                                { name: "Shop", id: "/shop" }
                            ]}
                            bottomLeftText="Luxury Hospitality Since 1995"
                            bottomRightText="reservations@prestigehotels.com"
                            button={{ text: "Cart", onClick: () => setCartOpen(true) }}
                        />
                    </div>
                    <main className="min-h-screen flex items-center justify-center pt-20">
                        <p className="text-foreground">Loading products...</p>
                    </main>
                    <div id="footer" data-section="footer">
                        <FooterSimple
                            columns={[
                                {
                                    title: "Navigate",                                    items: [
                                        { label: "Home", href: "/" },
                                        { label: "Rooms & Suites", href: "rooms" },
                                        { label: "Amenities", href: "amenities" },
                                        { label: "About Us", href: "about" }
                                    ]
                                },
                                {
                                    title: "Guest Services",                                    items: [
                                        { label: "Reservations", href: "contact" },
                                        { label: "Contact Us", href: "mailto:reservations@prestigehotels.com" },
                                        { label: "Special Requests", href: "contact" },
                                        { label: "Concierge", href: "contact" }
                                    ]
                                },
                                {
                                    title: "Company",                                    items: [
                                        { label: "About Prestige", href: "about" },
                                        { label: "Careers", href: "#" },
                                        { label: "Press", href: "#" },
                                        { label: "Sustainability", href: "#" }
                                    ]
                                },
                                {
                                    title: "Legal",                                    items: [
                                        { label: "Privacy Policy", href: "#" },
                                        { label: "Terms & Conditions", href: "#" },
                                        { label: "Accessibility", href: "#" }
                                    ]
                                }
                            ]}
                            bottomLeftText="© 2025 Prestige Hotels. All rights reserved."
                            bottomRightText="Luxury Hospitality Excellence"
                        />
                    </div>
                </ReactLenis>
            </ThemeProvider>
        );
    }

    return (
        <ThemeProvider
            defaultButtonVariant="slide-background"
            defaultTextAnimation="entrance-slide"
            borderRadius="pill"
            contentWidth="mediumSmall"
            sizing="mediumLarge"
            background="circleGradient"
            cardStyle="glass-elevated"
            primaryButtonStyle="diagonal-gradient"
            secondaryButtonStyle="layered"
            headingFontWeight="normal"
        >
            <ReactLenis root>
                <div id="navbar" data-section="navbar">
                    <NavbarStyleFullscreen
                        brandName="Prestige Hotels"
                        navItems={[
                            { name: "Home", id: "/" },
                            { name: "Rooms", id: "rooms" },
                            { name: "Amenities", id: "amenities" },
                            { name: "About", id: "about" },
                            { name: "Contact", id: "contact" },
                            { name: "Shop", id: "/shop" }
                        ]}
                        bottomLeftText="Luxury Hospitality Since 1995"
                        bottomRightText="reservations@prestigehotels.com"
                        button={{ text: "Cart", onClick: () => setCartOpen(true) }}
                    />
                </div>
                <div id="productCatalog" data-section="productCatalog">
                    <ProductCatalog
                        layout="page"
                        products={products}
                        searchValue={search}
                        onSearchChange={setSearch}
                        searchPlaceholder="Search products..."
                        filters={filters}
                        emptyMessage="No products found"
                    />
                </div>
                <div id="productCart" data-section="productCart">
                    <ProductCart
                        isOpen={cartOpen}
                        onClose={() => setCartOpen(false)}
                        items={cartItems}
                        onQuantityChange={updateQuantity}
                        onRemove={removeItem}
                        total={`$${cartTotal}`}
                        buttons={[
                            {
                                text: isCheckoutLoading ? "Processing..." : "Check Out",                                onClick: handleCheckout,
                            },
                        ]}
                    />
                </div>
                <div id="footer" data-section="footer">
                    <FooterSimple
                        columns={[
                            {
                                title: "Navigate",                                items: [
                                    { label: "Home", href: "/" },
                                    { label: "Rooms & Suites", href: "rooms" },
                                    { label: "Amenities", href: "amenities" },
                                    { label: "About Us", href: "about" }
                                ]
                            },
                            {
                                title: "Guest Services",                                items: [
                                    { label: "Reservations", href: "contact" },
                                    { label: "Contact Us", href: "mailto:reservations@prestigehotels.com" },
                                    { label: "Special Requests", href: "contact" },
                                    { label: "Concierge", href: "contact" }
                                ]
                            },
                            {
                                title: "Company",                                items: [
                                    { label: "About Prestige", href: "about" },
                                    { label: "Careers", href: "#" },
                                    { label: "Press", href: "#" },
                                    { label: "Sustainability", href: "#" }
                                ]
                            },
                            {
                                title: "Legal",                                items: [
                                    { label: "Privacy Policy", href: "#" },
                                    { label: "Terms & Conditions", href: "#" },
                                    { label: "Accessibility", href: "#" }
                                ]
                            }
                        ]}
                        bottomLeftText="© 2025 Prestige Hotels. All rights reserved."
                        bottomRightText="Luxury Hospitality Excellence"
                    />
                </div>
            </ReactLenis>
        </ThemeProvider>
    );
}