import { WCProduct, WCCategory } from '../services/woocommerce';

export const MOCK_WC_CATEGORIES: WCCategory[] = [
  // Top Level Categories (from timenlife.shop database)
  {
    id: 217,
    name: 'Apparel & Accessories',
    slug: 'apparel-accessories',
    parent: 0,
    description: 'Women’s Shalwar Kameez, Suits, Kurtis, Men’s Fashion, Jewelry, Handbags, Wallets & Accessories',
    image: { src: 'https://images.unsplash.com/photo-1584917865442-de89df76afd3?auto=format&fit=crop&w=800&q=80', alt: 'Apparel & Accessories' },
    count: 2708,
  },
  {
    id: 428,
    name: 'Home & Garden',
    slug: 'home-garden',
    parent: 0,
    description: 'Kitchen Accessories, Appliances, Dinnerware, Bath, Clocks, Wall Decor & Household Organizers',
    image: { src: 'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80', alt: 'Home & Garden' },
    count: 1340,
  },
  {
    id: 75,
    name: 'Health & Beauty',
    slug: 'health-beauty',
    parent: 0,
    description: 'Cosmetics, Skincare, Colognes & Perfumes, Trimmers, Hair Care, Massage & Medical Supplies',
    image: { src: 'https://images.unsplash.com/photo-1522337360788-8b13dee7a37e?auto=format&fit=crop&w=800&q=80', alt: 'Health & Beauty' },
    count: 319,
  },
  {
    id: 316,
    name: 'Electronics',
    slug: 'electronics',
    parent: 0,
    description: 'Audio Headphones, Speakers, Power Adapters, UPS, Computer Accessories & Gadgets',
    image: { src: 'https://images.unsplash.com/photo-1508685096489-7aacd43bd3b1?auto=format&fit=crop&w=800&q=80', alt: 'Electronics' },
    count: 154,
  },
  {
    id: 1599,
    name: 'Hardware & Tools',
    slug: 'hardware',
    parent: 0,
    description: 'Hand Tool Sets, Measuring Scales, Electrical Switches, Lighters & Fasteners',
    image: { src: 'https://images.unsplash.com/photo-1581092160607-ee22621dd758?auto=format&fit=crop&w=800&q=80', alt: 'Hardware & Tools' },
    count: 23,
  },
  {
    id: 1571,
    name: 'Vehicle & Parts',
    slug: 'vehicle-parts',
    parent: 0,
    description: 'Motor Vehicle Lighting, Vehicle Covers, Decor Accessories & Maintenance Tools',
    image: { src: 'https://images.unsplash.com/photo-1486006920555-c77dce18193b?auto=format&fit=crop&w=800&q=80', alt: 'Vehicle & Parts' },
    count: 24,
  },
  {
    id: 103,
    name: 'Food, Beverages & Tobacco',
    slug: 'food-beverages-tobacco',
    parent: 0,
    description: 'Pure Organic Honey, Pickles, Meats, Sweets & Vaporizer Accessories',
    image: { src: 'https://images.unsplash.com/photo-1587049352847-81a56d773cae?auto=format&fit=crop&w=800&q=80', alt: 'Food & Beverages' },
    count: 30,
  },
  {
    id: 874,
    name: 'Stationery & Craft',
    slug: 'stationery-craft-2',
    parent: 0,
    description: 'Decorative tapes, stickers, office supplies & creative crafting materials',
    image: { src: 'https://images.unsplash.com/photo-1456735190827-d1262f71b8a3?auto=format&fit=crop&w=800&q=80', alt: 'Stationery & Craft' },
    count: 31,
  },

  // Sub Categories - Apparel & Accessories
  {
    id: 1522,
    name: 'Clothing',
    slug: 'clothing',
    parent: 217,
    description: 'Suits, Shalwar Kameez, Kurtis, Sleepwear, Night Suits & Inner Wear',
    image: { src: 'https://images.unsplash.com/photo-1489987707025-afc232f7ea0f?auto=format&fit=crop&w=600&q=80', alt: 'Clothing' },
    count: 575,
  },
  {
    id: 866,
    name: "Women's Fashion",
    slug: 'womens-fashion',
    parent: 1522,
    description: 'Shalwar Kameez, Kurtis, Maxi, Dupattas & Winter Clothing',
    image: { src: 'https://images.unsplash.com/photo-1610030469983-98e550d6193c?auto=format&fit=crop&w=600&q=80', alt: "Women's Fashion" },
    count: 462,
  },
  {
    id: 954,
    name: 'Shalwar Kameez',
    slug: 'shalwar-kameez',
    parent: 866,
    description: 'Unstitched & Stitched 3-Piece Designer Lawn, Chiffon & Cotton Suits',
    image: { src: 'https://images.unsplash.com/photo-1583391733956-3750e0ff4e8b?auto=format&fit=crop&w=600&q=80', alt: 'Shalwar Kameez' },
    count: 311,
  },
  {
    id: 799,
    name: "Men's Fashion",
    slug: 'mens-fashion',
    parent: 1522,
    description: "Men's Suiting, Kurta Kameez, Inner Wear & Winter Apparel",
    image: { src: 'https://images.unsplash.com/photo-1617137984095-74e4e5e3613f?auto=format&fit=crop&w=600&q=80', alt: "Men's Fashion" },
    count: 53,
  },
  {
    id: 30,
    name: 'Jewelry & Watches',
    slug: 'jewelry',
    parent: 217,
    description: 'Earrings, Bracelets, Analog & Smartwatches, Rings & Jewelry Sets',
    image: { src: 'https://images.unsplash.com/photo-1535632066927-ab7c9ab60908?auto=format&fit=crop&w=600&q=80', alt: 'Jewelry' },
    count: 121,
  },
  {
    id: 114,
    name: 'Handbag, Wallets & Cases',
    slug: 'handbag-wallets-cases',
    parent: 217,
    description: 'Handbags, Crossbody Shoulder Bags, Leather Wallets & Money Clips',
    image: { src: 'https://images.unsplash.com/photo-1590874103328-eac38a683ce7?auto=format&fit=crop&w=600&q=80', alt: 'Handbags & Wallets' },
    count: 43,
  },

  // Sub Categories - Health & Beauty
  {
    id: 869,
    name: 'Personal Care',
    slug: 'personal-care',
    parent: 75,
    description: 'Cosmetics, Perfumes, Shaving, Hair Care & Electric Massagers',
    image: { src: 'https://images.unsplash.com/photo-1556228720-195a672e8a03?auto=format&fit=crop&w=600&q=80', alt: 'Personal Care' },
    count: 565,
  },
  {
    id: 326,
    name: 'Cosmetics & Fragrances',
    slug: 'cosmetics',
    parent: 869,
    description: 'Colognes & Perfumes, Foundations, Skin Care Serums & Lips Makeup',
    image: { src: 'https://images.unsplash.com/photo-1570172619644-dfd03ed5d881?auto=format&fit=crop&w=600&q=80', alt: 'Cosmetics' },
    count: 111,
  },
  {
    id: 774,
    name: 'Shaving & Trimmers',
    slug: 'shaving-grooming',
    parent: 869,
    description: 'VGR Professional Hair Clippers, Rechargeable T-Blade Trimmers & Epilators',
    image: { src: 'https://images.unsplash.com/photo-1621607512214-68297480165e?auto=format&fit=crop&w=600&q=80', alt: 'Shaving & Grooming' },
    count: 42,
  },
  {
    id: 784,
    name: 'Hair Care & Tools',
    slug: 'hair-care',
    parent: 869,
    description: 'Hair Straighteners, Curlers, Hair Oils, Dryers & Shampoos',
    image: { src: 'https://images.unsplash.com/photo-1522337360788-8b13dee7a37e?auto=format&fit=crop&w=600&q=80', alt: 'Hair Care' },
    count: 38,
  },

  // Sub Categories - Home & Garden
  {
    id: 860,
    name: 'Kitchen Accessories',
    slug: 'kitchen-accessories',
    parent: 428,
    description: 'Slicers, Oil Dispensers, Storage Jars, Water Bottles & Cookware Utensils',
    image: { src: 'https://images.unsplash.com/photo-1556911220-e15b29be8c8f?auto=format&fit=crop&w=600&q=80', alt: 'Kitchen Accessories' },
    count: 284,
  },
  {
    id: 429,
    name: 'Decor & Clocks',
    slug: 'decor',
    parent: 428,
    description: 'Wall Clocks, Desk Clocks, Picture Frames & Acrylic Mirrors',
    image: { src: 'https://images.unsplash.com/photo-1513519245088-0e12902e5a38?auto=format&fit=crop&w=600&q=80', alt: 'Decor' },
    count: 47,
  },

  // Sub Categories - Electronics
  {
    id: 1541,
    name: 'Audio & Headphones',
    slug: 'audio',
    parent: 316,
    description: 'Wireless Bluetooth Earphones, Headsets, Portable Speakers & Microphones',
    image: { src: 'https://images.unsplash.com/photo-1505740420928-5e560c06d30e?auto=format&fit=crop&w=600&q=80', alt: 'Audio' },
    count: 50,
  },
  {
    id: 1562,
    name: 'Power & Adapters',
    slug: 'power',
    parent: 316,
    description: 'Power Supply Converters, Fast Chargers, UPS & Charging Cables',
    image: { src: 'https://images.unsplash.com/photo-1583863788434-e58a36330cf0?auto=format&fit=crop&w=600&q=80', alt: 'Power & Adapters' },
    count: 47,
  },
];

// All realistic products matching the Time N Life WooCommerce catalog
export const MOCK_ALL_PRODUCTS: WCProduct[] = Array.from({ length: 75 }, (_, index) => {
  const id = 1000 + index + 1;
  const num = index + 1;

  // Distribute across top categories & subcategories
  let category: { id: number; name: string; slug: string };
  let topCategory: { id: number; name: string; slug: string };
  let imageSrc: string;
  let title: string;
  let regularPriceNum: number;
  let salePriceNum: number;
  let shortDesc: string;

  if (num % 4 === 1) {
    topCategory = { id: 101, name: 'Health & Beauty', slug: 'health-beauty' };
    if (num % 3 === 0) {
      category = { id: 201, name: 'Grooming & Trimmers', slug: 'grooming-trimmers' };
    } else if (num % 3 === 1) {
      category = { id: 202, name: 'Skin Care & Cosmetics', slug: 'skin-care-cosmetics' };
    } else {
      category = { id: 203, name: 'Biometric & Health Monitors', slug: 'health-monitors' };
    }
    imageSrc = [
      'https://images.unsplash.com/photo-1621607512214-68297480165e?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1570172619644-dfd03ed5d881?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1576091160399-112ba8d25d1d?auto=format&fit=crop&w=800&q=80',
    ][num % 3];
    title = `VGR Professional Precision Grooming Kit ${num}`;
    regularPriceNum = 4500 + (num * 110);
    salePriceNum = 2999 + (num * 75);
    shortDesc = 'Cordless rechargeable stainless steel T-blade grooming trimmer & health care device.';
  } else if (num % 4 === 2) {
    topCategory = { id: 102, name: 'Fashion & Lifestyle', slug: 'fashion-lifestyle' };
    if (num % 3 === 0) {
      category = { id: 204, name: 'Handbags & Purses', slug: 'handbags-purses' };
    } else if (num % 3 === 1) {
      category = { id: 205, name: 'Wallets & Belts', slug: 'wallets-belts' };
    } else {
      category = { id: 206, name: 'Sunglasses & Eyewear', slug: 'sunglasses-eyewear' };
    }
    imageSrc = [
      'https://images.unsplash.com/photo-1584917865442-de89df76afd3?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1590874103328-eac38a683ce7?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1511499767150-a48a237f0083?auto=format&fit=crop&w=800&q=80',
    ][num % 3];
    title = `Luxury Designer Fashion Accessory ${num}`;
    regularPriceNum = 6800 + (num * 140);
    salePriceNum = 4200 + (num * 85);
    shortDesc = 'Handcrafted premium leather & stylish luxury fashion collection.';
  } else if (num % 4 === 3) {
    topCategory = { id: 103, name: 'Watches & Accessories', slug: 'watches-accessories' };
    if (num % 3 === 0) {
      category = { id: 207, name: 'Analog Watches', slug: 'analog-watches' };
    } else if (num % 3 === 1) {
      category = { id: 208, name: 'Smartwatches', slug: 'smartwatches' };
    } else {
      category = { id: 209, name: 'Jewelry & Care', slug: 'jewelry-care' };
    }
    imageSrc = [
      'https://images.unsplash.com/photo-1522335789203-aabd1fc54bc9?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1524805444758-089113d48a6d?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1508685096489-7aacd43bd3b1?auto=format&fit=crop&w=800&q=80',
    ][num % 3];
    title = `Chronograph Timepiece & Smart Device ${num}`;
    regularPriceNum = 8900 + (num * 180);
    salePriceNum = 5499 + (num * 105);
    shortDesc = 'Water resistant precision quartz & AMOLED display smartwatch with luxury finish.';
  } else {
    topCategory = { id: 104, name: 'Home & Kitchen', slug: 'home-kitchen' };
    category = { id: 104, name: 'Home & Kitchen', slug: 'home-kitchen' };
    imageSrc = [
      'https://images.unsplash.com/photo-1513694203232-719a280e022f?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1522337360788-8b13dee7a37e?auto=format&fit=crop&w=800&q=80',
      'https://images.unsplash.com/photo-1584917865442-de89df76afd3?auto=format&fit=crop&w=800&q=80',
    ][num % 3];
    title = `Smart Home Ambient Organiser & Appliance ${num}`;
    regularPriceNum = 5200 + (num * 130);
    salePriceNum = 3100 + (num * 80);
    shortDesc = 'Aroma diffuser, linen organiser and smart decor accents for modern living.';
  }

  const isOnSale = num % 2 === 0;
  const currentPrice = isOnSale ? salePriceNum : regularPriceNum;

  return {
    id,
    name: title,
    slug: title.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/(^-|-$)/g, ''),
    price: currentPrice.toString(),
    regular_price: regularPriceNum.toString(),
    sale_price: isOnSale ? salePriceNum.toString() : '',
    on_sale: isOnSale,
    stock_status: num % 12 === 0 ? 'outofstock' : 'instock',
    rating_count: 5 + (num % 25),
    average_rating: (4.2 + (num % 8) * 0.1).toFixed(1),
    sku: `TNL-${1000 + num}`,
    short_description: shortDesc,
    description: `
      <p>Elevate your lifestyle with the <strong>${title}</strong>, imported exclusively for <em>Time N Life</em>. Designed with premium grade durability and fine finish to deliver maximum performance.</p>
      <h4>Key Features & Specs:</h4>
      <ul>
        <li>Premium grade build quality with rust-resistant alloy finish</li>
        <li>1-Year Time N Life Brand Warranty Included</li>
        <li>Compact & ergonomic design for daily portable use</li>
        <li>Quick Delivery across Pakistan with Cash on Delivery (COD) available</li>
      </ul>
      <p>Order now to enjoy special discounted prices and hassle-free returns!</p>
    `,
    images: [
      { id: 1, src: imageSrc, alt: title },
      { id: 2, src: 'https://images.unsplash.com/photo-1523275335684-37898b6baf30?auto=format&fit=crop&w=800&q=80', alt: `${title} side view` },
      { id: 3, src: 'https://images.unsplash.com/photo-1522337360788-8b13dee7a37e?auto=format&fit=crop&w=800&q=80', alt: `${title} box packaging` },
    ],
    categories: [
      { id: category.id, name: category.name, slug: category.slug },
      { id: topCategory.id, name: topCategory.name, slug: topCategory.slug },
    ],
    permalink: `https://timenlife.shop/product/${title.toLowerCase().replace(/[^a-z0-9]+/g, '-')}`,
    attributes: [
      { name: 'Warranty', options: ['1 Year Local Brand Warranty'] },
      { name: 'Shipping', options: ['Free COD Nationwide over Rs 2,500'] },
      { name: 'Condition', options: ['100% Brand New Sealed'] },
    ],
  };
});

export const MOCK_50_PRODUCTS = MOCK_ALL_PRODUCTS;

// Helper to generate realistic paginated products up to 7500+ items for any page number
export function generateMockProductsForPage(
  page: number = 1,
  perPage: number = 24,
  categoryId?: number | string | null,
  searchQuery: string = ''
): { products: WCProduct[]; totalProducts: number; totalPages: number } {
  const TOTAL_PRODUCTS = 7500;
  const totalPages = Math.ceil(TOTAL_PRODUCTS / perPage);
  const safePage = Math.max(1, Math.min(page, totalPages));

  // Base products array created deterministically based on safePage
  const products: WCProduct[] = Array.from({ length: perPage }, (_, i) => {
    const itemIndex = (safePage - 1) * perPage + i + 1;
    const baseTemplate = MOCK_ALL_PRODUCTS[i % MOCK_ALL_PRODUCTS.length];
    
    // Assign specific category if filtered
    let assignedCats = baseTemplate.categories;
    if (categoryId) {
      const catNum = Number(categoryId);
      const matchedCat = MOCK_WC_CATEGORIES.find((c) => c.id === catNum);
      if (matchedCat) {
        assignedCats = [
          { id: matchedCat.id, name: matchedCat.name, slug: matchedCat.slug },
          { id: matchedCat.parent || 101, name: 'Main Category', slug: 'main' }
        ];
      }
    }

    const id = 10000 + itemIndex;
    const name = categoryId
      ? `${baseTemplate.name.split('#')[0]} Spec ${itemIndex}`
      : `${baseTemplate.name.replace(/\d+$/, '')} Item #${itemIndex}`;

    return {
      ...baseTemplate,
      id,
      name,
      slug: `product-${id}`,
      sku: `TNL-${id}`,
      categories: assignedCats,
      permalink: `https://timenlife.shop/product/item-${id}`,
    };
  });

  // Filter if searchQuery provided
  let filtered = products;
  if (searchQuery.trim()) {
    const q = searchQuery.toLowerCase();
    filtered = products.filter(
      (p) =>
        p.name.toLowerCase().includes(q) ||
        p.short_description?.toLowerCase().includes(q) ||
        p.categories.some((c) => c.name.toLowerCase().includes(q))
    );
  }

  return {
    products: filtered,
    totalProducts: TOTAL_PRODUCTS,
    totalPages,
  };
}
