"use client";

import ReactLenis from "lenis/react";
import { ThemeProvider } from "@/providers/themeProvider/ThemeProvider";
import NavbarStyleCentered from '@/components/navbar/NavbarStyleCentered/NavbarStyleCentered';
import ProductCatalog from "@/components/ecommerce/productCatalog/ProductCatalog";
import { useProductCatalog } from "@/hooks/useProductCatalog";
import { useCart } from "@/hooks/useCart";
import { useCheckout } from "@/hooks/useCheckout";
import { useCallback } from "react";
import ProductCart from "@/components/ecommerce/cart/ProductCart";
import FooterCard from '@/components/sections/footer/FooterCard';
import { Instagram, Facebook, Twitter } from 'lucide-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="hover-bubble"
                defaultTextAnimation="reveal-blur"
                borderRadius="soft"
                contentWidth="mediumLarge"
                sizing="large"
                background="fluid"
                cardStyle="soft-shadow"
                primaryButtonStyle="gradient"
                secondaryButtonStyle="glass"
                headingFontWeight="bold"
            >
                <ReactLenis root>
                    <div id="navbar" data-section="navbar">
                        <NavbarStyleCentered
                            brandName="Paw Haven"
                            navItems={[
                                { name: "Home", id: "/" },
                                { name: "Adopt", id: "/#adopt" },
                                { name: "About", id: "/#about" },
                                { name: "Success", id: "/#testimonials" },
                                { name: "Support", id: "/#support" },
                                { name: "Blog", id: "/blog" },
                                { name: "Shop", id: "/shop" },
                                { name: "Contact", id: "/#contact" }
                            ]}
                            button={{ text: "Cart", onClick: () => setCartOpen(true) }}
                        />
                    </div>
                    <div id="loading-section" data-section="loading-section">
                        <main className="min-h-screen flex items-center justify-center pt-20">
                            <p className="text-foreground">Loading products...</p>
                        </main>
                    </div>
                    <div id="footer" data-section="footer">
                        <FooterCard
                            logoText="Paw Haven"
                            copyrightText="© 2024 Paw Haven. All rights reserved."
                            socialLinks={[
                                { icon: Instagram, href: "https://instagram.com/pawhaven", ariaLabel: "Instagram" },
                                { icon: Facebook, href: "https://facebook.com/pawhaven", ariaLabel: "Facebook" },
                                { icon: Twitter, href: "https://twitter.com/pawhaven", ariaLabel: "Twitter" }
                            ]}
                            ariaLabel="Paw Haven Footer"
                        />
                    </div>
                </ReactLenis>
            </ThemeProvider>
        );
    }

    return (
        <ThemeProvider
            defaultButtonVariant="hover-bubble"
            defaultTextAnimation="reveal-blur"
            borderRadius="soft"
            contentWidth="mediumLarge"
            sizing="large"
            background="fluid"
            cardStyle="soft-shadow"
            primaryButtonStyle="gradient"
            secondaryButtonStyle="glass"
            headingFontWeight="bold"
        >
            <ReactLenis root>
                <div id="navbar" data-section="navbar">
                    <NavbarStyleCentered
                        brandName="Paw Haven"
                        navItems={[
                            { name: "Home", id: "/" },
                            { name: "Adopt", id: "/#adopt" },
                            { name: "About", id: "/#about" },
                            { name: "Success", id: "/#testimonials" },
                            { name: "Support", id: "/#support" },
                            { name: "Blog", id: "/blog" },
                            { name: "Shop", id: "/shop" },
                            { name: "Contact", id: "/#contact" }
                        ]}
                        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">
                    <FooterCard
                        logoText="Paw Haven"
                        copyrightText="© 2024 Paw Haven. All rights reserved."
                        socialLinks={[
                            { icon: Instagram, href: "https://instagram.com/pawhaven", ariaLabel: "Instagram" },
                            { icon: Facebook, href: "https://facebook.com/pawhaven", ariaLabel: "Facebook" },
                            { icon: Twitter, href: "https://twitter.com/pawhaven", ariaLabel: "Twitter" }
                        ]}
                        ariaLabel="Paw Haven Footer"
                    />
                </div>
            </ReactLenis>
        </ThemeProvider>
    );
}
