'use client';

import React, { useState, useEffect } from 'react';
import ProductCard from './product-card';
import { getProducts } from '@/hooks/use-shopify-products';
import { Button } from '@/components/ui/button';
import { Loader } from '@/components/ui/loader';

interface ProductImage {
  url: string;
  altText?: string;
}

interface ProductPrice {
  amount: string;
  currencyCode: string;
}

interface ProductVariant {
  id: string;
  title: string;
  price: ProductPrice;
  availableForSale: boolean;
}

interface Product {
  id: string;
  title: string;
  description?: string;
  handle: string;
  images: {
    edges: Array<{
      node: ProductImage;
    }>;
  };
  priceRange: {
    minVariantPrice: ProductPrice;
  };
  compareAtPriceRange?: {
    minVariantPrice: ProductPrice;
  };
  variants: {
    edges: Array<{
      node: ProductVariant;
    }>;
  };
}

interface ProductsProps {
  title?: string;
  limit?: number;
  showLoadMore?: boolean;
}

const Products: React.FC<ProductsProps> = ({ 
  title = "Shop All", 
  limit = 12,
  showLoadMore = true 
}) => {
  const [products, setProducts] = useState<Product[]>([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
  const [loadingMore, setLoadingMore] = useState(false);
  const [hasMoreProducts, setHasMoreProducts] = useState(true);

  const fetchProducts = async (currentProducts: Product[] = [], loadMore = false) => {
    try {
      if (loadMore) {
        setLoadingMore(true);
      } else {
        setLoading(true);
        setError(null);
      }

      const newProducts = await getProducts({ 
        first: limit,
        sortKey: 'CREATED_AT',
        reverse: true
      });

      if (loadMore) {
        // Filter out products that already exist
        const existingIds = new Set(currentProducts.map(p => p.id));
        const uniqueNewProducts = newProducts.filter(p => !existingIds.has(p.id));
        
        if (uniqueNewProducts.length === 0) {
          setHasMoreProducts(false);
        } else {
          setProducts(prev => [...prev, ...uniqueNewProducts]);
        }
      } else {
        setProducts(newProducts);
        setHasMoreProducts(newProducts.length === limit);
      }
    } catch (err) {
      console.error('Error fetching products:', err);
      setError(err instanceof Error ? err.message : 'Failed to load products');
    } finally {
      setLoading(false);
      setLoadingMore(false);
    }
  };

  useEffect(() => {
    fetchProducts();
  }, [limit]);

  const handleAddToCart = async (product: Product) => {
    // Here you would typically integrate with cart functionality
    console.log('Adding to cart:', product);
  };

  const handleLoadMore = () => {
    if (!loadingMore && hasMoreProducts) {
      fetchProducts(products, true);
    }
  };

  if (loading) {
    return (
      <div className="py-16">
        <div className="container mx-auto px-4">
          <h2 className="text-4xl font-medium tracking-tight text-center mb-12 text-gray-900 font-heading">
            {title}
          </h2>
          
          {/* Loading Skeleton */}
          <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8">
            {Array.from({ length: 6 }).map((_, index) => (
              <div key={index} className="animate-pulse">
                <div className="aspect-square bg-gray-200"></div>
                <div className="mt-3 h-4 w-2/3 bg-gray-200 rounded"></div>
                <div className="mt-2 h-4 w-1/4 bg-gray-200 rounded"></div>
              </div>
            ))}
          </div>
        </div>
      </div>
    );
  }

  if (error) {
    return (
      <div className="py-16">
        <div className="container mx-auto px-4 py-24 text-center">
          <h2 className="text-2xl font-medium tracking-tight text-gray-900 font-heading mb-3">
            Products Unavailable
          </h2>
          <p className="text-gray-500 max-w-md mx-auto">
            We couldn&apos;t load any products right now. Please try again
            later.
          </p>
        </div>
      </div>
    );
  }

  if (products.length === 0) {
    return (
      <div className="py-16">
        <div className="container mx-auto px-4 text-center">
          <h2 className="text-4xl font-medium tracking-tight mb-8 text-gray-900 font-heading">
            {title}
          </h2>
          
          <div className="max-w-md mx-auto py-12">
            <h3 className="text-lg font-medium text-gray-900 mb-2">
              No Products Found
            </h3>
            <p className="text-gray-500">
              Check back later or configure your Shopify store connection.
            </p>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="py-16">
      <div className="container mx-auto px-4">
        <h2 className="text-4xl font-medium tracking-tight text-center mb-12 text-gray-900 font-heading">
          {title}
        </h2>

        {/* Products Grid */}
        <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-8 mb-12">
          {products.map((product) => (
            <ProductCard
              key={product.id}
              product={product}
              onAddToCart={handleAddToCart}
            />
          ))}
        </div>

        {/* Load More Button */}
        {showLoadMore && hasMoreProducts && (
          <div className="text-center">
            <Button
              onClick={handleLoadMore}
              disabled={loadingMore}
              size="lg"
              className="font-heading"
            >
              {loadingMore ? (
                <span className="flex items-center space-x-2">
                  <Loader size={16} />
                  <span>Loading...</span>
                </span>
              ) : (
                'Load More Products'
              )}
            </Button>
          </div>
        )}
      </div>
    </div>
  );
};

export default Products;