"use client";

import ReactLenis from "lenis/react";
import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider";
import NavbarStyleFullscreen from '@/components/navbar/NavbarStyleFullscreen/NavbarStyleFullscreen';
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";
import { useCheckout } from "@/hooks/useCheckout";
import { useCallback } from "react";

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 = useCallback(async () => {
        if (cartItems.length === 0) return;

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

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

    if (isLoading) {
        return (
            <ThemeProvider
                defaultButtonVariant="expand-hover"
                defaultTextAnimation="background-highlight"
                borderRadius="soft"
                contentWidth="mediumLarge"
                sizing="mediumLargeSizeMediumTitles"
                background="circleGradient"
                cardStyle="gradient-bordered"
                primaryButtonStyle="gradient"
                secondaryButtonStyle="solid"
                headingFontWeight="semibold"
            >
                <ReactLenis root>
                    <div id="navbar" data-section="navbar">
                        <NavbarStyleFullscreen
                            navItems={[{ name: "Home", id: "/" }, { name: "Shop", id: "/shop" }]}
                            brandName="Custom European Landscaping"
                            bottomLeftText="Timeless Craftsmanship"
                            bottomRightText="(XXX) XXX-XXXX"
                            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
                            columns={[
                                { items: [{ label: "Home", href: "/" }, { label: "About Us", href: "#about" }, { label: "Our Services", href: "#services" }, { label: "View Our Work", href: "#work" }] },
                                { items: [{ label: "Client Reviews", href: "#testimonials" }, { label: "Our Process", href: "#process" }, { label: "Request Consultation", href: "#contact" }, { label: "(XXX) XXX-XXXX", href: "tel:XXX-XXX-XXXX" }] }
                            ]}
                            logoText="Custom European Landscaping"
                            logoClassName="text-4xl font-serif text-foreground mb-4"
                            columnClassName="text-foreground/80"
                            itemClassName="text-foreground/70 hover:text-foreground transition-colors"
                            containerClassName="border-t-2 border-border pt-12 pb-8"
                            className="bg-card"
                        />
                    </div>
                </ReactLenis>
            </ThemeProvider>
        );
    }

    return (
        <ThemeProvider
            defaultButtonVariant="expand-hover"
            defaultTextAnimation="background-highlight"
            borderRadius="soft"
            contentWidth="mediumLarge"
            sizing="mediumLargeSizeMediumTitles"
            background="circleGradient"
            cardStyle="gradient-bordered"
            primaryButtonStyle="gradient"
            secondaryButtonStyle="solid"
            headingFontWeight="semibold"
        >
            <ReactLenis root>
                <div id="navbar" data-section="navbar">
                    <NavbarStyleFullscreen
                        navItems={[{ name: "Home", id: "/" }, { name: "Shop", id: "/shop" }]}
                        brandName="Custom European Landscaping"
                        bottomLeftText="Timeless Craftsmanship"
                        bottomRightText="(XXX) XXX-XXXX"
                        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>
                <ProductCart
                    isOpen={cartOpen}
                    onClose={() => setCartOpen(false)}
                    items={cartItems}
                    onQuantityChange={updateQuantity}
                    onRemove={removeItem}
                    total={`$${cartTotal}`}
                    buttons={[
                        {
                            text: isCheckoutLoading ? "Processing..." : "Check Out",                            onClick: handleCheckout,
                        },
                    ]}
                />
                <div id="footer" data-section="footer">
                    <FooterLogoEmphasis
                        columns={[
                            { items: [{ label: "Home", href: "/" }, { label: "About Us", href: "#about" }, { label: "Our Services", href: "#services" }, { label: "View Our Work", href: "#work" }] },
                            { items: [{ label: "Client Reviews", href: "#testimonials" }, { label: "Our Process", href: "#process" }, { label: "Request Consultation", href: "#contact" }, { label: "(XXX) XXX-XXXX", href: "tel:XXX-XXX-XXXX" }] }
                        ]}
                        logoText="Custom European Landscaping"
                        logoClassName="text-4xl font-serif text-foreground mb-4"
                        columnClassName="text-foreground/80"
                        itemClassName="text-foreground/70 hover:text-foreground transition-colors"
                        containerClassName="border-t-2 border-border pt-12 pb-8"
                        className="bg-card"
                    />
                </div>
            </ReactLenis>
        </ThemeProvider>
    );
}