"use client";

import { useState } from "react";
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 ProductCart from "@/components/ecommerce/cart/ProductCart";
import { useProductCatalog } from "@/hooks/useProductCatalog";
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,
        addItem,
        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="bounce-effect"
                defaultTextAnimation="entrance-slide"
                borderRadius="sharp"
                contentWidth="small"
                sizing="mediumSizeLargeTitles"
                background="circleGradient"
                cardStyle="outline"
                primaryButtonStyle="gradient"
                secondaryButtonStyle="layered"
                headingFontWeight="medium"
            >
                <ReactLenis root>
                    <div id="navbar" data-section="navbar">
                        <NavbarStyleFullscreen
                            navItems={[
                                { name: "Home", id: "/" },
                                { name: "About", id: "about" },
                                { name: "Menu", id: "menu" },
                                { name: "Gallery", id: "gallery" },
                                { name: "Reserve", id: "reservations" },
                                { name: "Shop", id: "/shop" }
                            ]}
                            brandName="Elegance"
                            bottomLeftText="Fine Dining Experience"
                            bottomRightText="info@elegance.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: "hero" },
                                        { label: "About Us", href: "about" },
                                        { label: "Menu", href: "menu" }
                                    ]
                                },
                                {
                                    title: "Dine With Us",                                    items: [
                                        { label: "Reservations", href: "reservations" },
                                        { label: "Gallery", href: "gallery" },
                                        { label: "Private Events", href: "#" }
                                    ]
                                },
                                {
                                    title: "Contact",                                    items: [
                                        { label: "Hours", href: "#" },
                                        { label: "Location", href: "#" },
                                        { label: "Phone", href: "#" }
                                    ]
                                }
                            ]}
                            bottomLeftText="© 2025 Elegance Restaurant. All rights reserved."
                            bottomRightText="Crafted with culinary passion"
                        />
                    </div>
                </ReactLenis>
            </ThemeProvider>
        );
    }

    return (
        <ThemeProvider
            defaultButtonVariant="bounce-effect"
            defaultTextAnimation="entrance-slide"
            borderRadius="sharp"
            contentWidth="small"
            sizing="mediumSizeLargeTitles"
            background="circleGradient"
            cardStyle="outline"
            primaryButtonStyle="gradient"
            secondaryButtonStyle="layered"
            headingFontWeight="medium"
        >
            <ReactLenis root>
                <div id="navbar" data-section="navbar">
                    <NavbarStyleFullscreen
                        navItems={[
                            { name: "Home", id: "/" },
                            { name: "About", id: "about" },
                            { name: "Menu", id: "menu" },
                            { name: "Gallery", id: "gallery" },
                            { name: "Reserve", id: "reservations" },
                            { name: "Shop", id: "/shop" }
                        ]}
                        brandName="Elegance"
                        bottomLeftText="Fine Dining Experience"
                        bottomRightText="info@elegance.com"
                        button={{ text: "Cart", onClick: () => setCartOpen(true) }}
                    />
                </div>
                <div id="product-catalog" data-section="product-catalog">
                    <ProductCatalog
                        layout="page"
                        products={products}
                        searchValue={search}
                        onSearchChange={setSearch}
                        searchPlaceholder="Search products..."
                        filters={filters}
                        emptyMessage="No products found"
                    />
                </div>
                <div id="product-cart" data-section="product-cart">
                    <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: "hero" },
                                    { label: "About Us", href: "about" },
                                    { label: "Menu", href: "menu" }
                                ]
                            },
                            {
                                title: "Dine With Us",                                items: [
                                    { label: "Reservations", href: "reservations" },
                                    { label: "Gallery", href: "gallery" },
                                    { label: "Private Events", href: "#" }
                                ]
                            },
                            {
                                title: "Contact",                                items: [
                                    { label: "Hours", href: "#" },
                                    { label: "Location", href: "#" },
                                    { label: "Phone", href: "#" }
                                ]
                            }
                        ]}
                        bottomLeftText="© 2025 Elegance Restaurant. All rights reserved."
                        bottomRightText="Crafted with culinary passion"
                    />
                </div>
            </ReactLenis>
        </ThemeProvider>
    );
}