"use client";

import { Suspense } from "react";
import ReactLenis from "lenis/react";
import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider";
import NavbarLayoutFloatingInline from '@/components/navbar/NavbarLayoutFloatingInline';
import FooterLogoEmphasis from '@/components/sections/footer/FooterLogoEmphasis';
import ProductCatalog from "@/components/ecommerce/productCatalog/ProductCatalog";
import { useProductCatalog } from "@/hooks/useProductCatalog";
import { useCart } from "@/hooks/useCart";
import ProductCart from "@/components/ecommerce/cart/ProductCart";

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

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

    if (isLoading) {
        return (
            <ThemeProvider
                defaultButtonVariant="text-stagger"
                defaultTextAnimation="background-highlight"
                borderRadius="rounded"
                contentWidth="smallMedium"
                sizing="mediumLargeSizeMediumTitles"
                background="grid"
                cardStyle="outline"
                primaryButtonStyle="double-inset"
                secondaryButtonStyle="glass"
                headingFontWeight="bold"
            >
                <ReactLenis root>
                    <div id="navbar" data-section="navbar">
                    <NavbarLayoutFloatingInline
                        brandName="Tony's Tire Shop"
                        navItems={[
                            { name: "Home", id: "/" },
                            { name: "Shop", id: "/shop" }
                        ]}
                        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">
                        <FooterLogoEmphasis
                            logoText="Tony's Tire Shop"
                            columns={[
                                {
                                    items: [
                                        { label: "Home", href: "/" },
                                        { label: "Tires", href: "/tires" },
                                        { label: "About", href: "/about" }
                                    ]
                                },
                                {
                                    items: [
                                        { label: "Contact", href: "/contact" },
                                        { label: "Call: (800) 123-4567", href: "tel:+1-800-123-4567" }
                                    ]
                                },
                                {
                                    items: [
                                        { label: "Hours: Mon-Fri 7am-6pm" },
                                        { label: "Hours: Sat 8am-4pm" },
                                        { label: "Sunday: Closed" }
                                    ]
                                },
                                {
                                    items: [
                                        { label: "Privacy Policy" },
                                        { label: "Terms of Service" },
                                        { label: "Warranty Info" },
                                        { label: "Financing" }
                                    ]
                                }
                            ]}
                        />
                    </div>
                </ReactLenis>
            </ThemeProvider>
        );
    }

    return (
        <ThemeProvider
            defaultButtonVariant="text-stagger"
            defaultTextAnimation="background-highlight"
            borderRadius="rounded"
            contentWidth="smallMedium"
            sizing="mediumLargeSizeMediumTitles"
            background="grid"
            cardStyle="outline"
            primaryButtonStyle="double-inset"
            secondaryButtonStyle="glass"
            headingFontWeight="bold"
        >
            <ReactLenis root>
                <div id="navbar" data-section="navbar">
                <NavbarLayoutFloatingInline
                    brandName="Tony's Tire Shop"
                    navItems={[
                        { name: "Home", id: "/" },
                        { name: "Shop", id: "/shop" }
                    ]}
                    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: "Check Out",                            onClick: () => console.log("checkout")
                        }
                    ]}
                />
                </div>
                <div id="footer" data-section="footer">
                    <FooterLogoEmphasis
                        logoText="Tony's Tire Shop"
                        columns={[
                            {
                                items: [
                                    { label: "Home", href: "/" },
                                    { label: "Tires", href: "/tires" },
                                    { label: "About", href: "/about" }
                                ]
                            },
                            {
                                items: [
                                    { label: "Contact", href: "/contact" },
                                    { label: "Call: (800) 123-4567", href: "tel:+1-800-123-4567" }
                                ]
                            },
                            {
                                items: [
                                    { label: "Hours: Mon-Fri 7am-6pm" },
                                    { label: "Hours: Sat 8am-4pm" },
                                    { label: "Sunday: Closed" }
                                ]
                            },
                            {
                                items: [
                                    { label: "Privacy Policy" },
                                    { label: "Terms of Service" },
                                    { label: "Warranty Info" },
                                    { label: "Financing" }
                                ]
                            }
                        ]}
                    />
                </div>
            </ReactLenis>
        </ThemeProvider>
    );
}

export default function ShopPage() {
    return (
        <Suspense>
            <ShopPageContent />
        </Suspense>
    );
}