/**
 * Products Module
 * Contains product data and product-related functionality
 */

// Configuration
const CONFIG = {
  businessName: 'Guarulhos Liquor Store',
  whatsappNumber: '+254722203960', // Replace with actual WhatsApp number
  paystackPublicKey: 'pk_live_07540fdb60b8b30e21fa2bb8249921a86b8ac169', // Live Paystack Public Key
  paystackSecretKey: 'sk_live_7f111b51a47aa90d451ea8f4311caa76fec83ecb', // Live Paystack Secret Key (for server-side)
  currency: 'KES',
  storeName: 'Guarulhos',
  callbackUrl: '/callback.php',
  webhookUrl: '/webhook.php',
};

// Product Categories
const CATEGORIES = {
  BEER: 'Beer',
  CANS: 'Cans',
  VODKA: 'Vodka',
  BRANDY: 'Brandy',
  SPIRIT: 'Spirit',
  GIN: 'Gin',
  WHISKEY: 'Whiskey',
  TEQUILA: 'Tequila',
  LIQUER: 'Liquer',
  WINE: 'Wine',
  SOFT_DRINKS: 'Soft Drinks',
};

// Sample Products Data
const PRODUCTS = [
  // Beer (5 products)
  { id: 1, name: 'T.Lager', category: CATEGORIES.BEER, price: 250, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Tusker Lager Beer' },
  { id: 2, name: 'Balozi', category: CATEGORIES.BEER, price: 250, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Balozi Beer' },
  { id: 3, name: 'G.K', category: CATEGORIES.BEER, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Guinness Kenya' },
  { id: 4, name: 'W/Cap', category: CATEGORIES.BEER, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'White Cap Beer' },
  { id: 5, name: 'T.Cider', category: CATEGORIES.BEER, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Tusker Cider' },

  // Cans (6 products)
  { id: 6, name: 'Guarana', category: CATEGORIES.CANS, price: 250, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Guarana Drink Can' },
  { id: 7, name: 'Gordons cans', category: CATEGORIES.CANS, price: 270, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Gordon\'s Gin Can' },
  { id: 8, name: 'Black Ice', category: CATEGORIES.CANS, price: 250, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Smirnoff Black Ice Can' },
  { id: 9, name: 'Snapp', category: CATEGORIES.CANS, price: 250, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Snapp Drink' },
  { id: 10, name: 'Pineapple punch', category: CATEGORIES.CANS, price: 250, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Pineapple Punch Drink' },
  { id: 11, name: 'Red Bull', category: CATEGORIES.CANS, price: 270, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Red Bull Energy Drink' },

  // Vodka (2 products)
  { id: 12, name: 'Smirnoff 750ml', category: CATEGORIES.VODKA, price: 1700, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Smirnoff Vodka 750ml' },
  { id: 13, name: 'Smirnoff 250ml', category: CATEGORIES.VODKA, price: 600, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Smirnoff Vodka 250ml' },

  // Brandy (8 products)
  { id: 14, name: 'Viceroy 750ml', category: CATEGORIES.BRANDY, price: 1700, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Viceroy Brandy 750ml' },
  { id: 15, name: 'Viceroy 250ml', category: CATEGORIES.BRANDY, price: 600, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Viceroy Brandy 250ml' },
  { id: 16, name: 'Richot 750ml', category: CATEGORIES.BRANDY, price: 1700, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Richot Brandy 750ml' },
  { id: 17, name: 'Richot 250ml', category: CATEGORIES.BRANDY, price: 600, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Richot Brandy 250ml' },
  { id: 18, name: 'County 750ml', category: CATEGORIES.BRANDY, price: 1000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'County Brandy 750ml' },
  { id: 19, name: 'County 250ml', category: CATEGORIES.BRANDY, price: 320, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'County Brandy 250ml' },
  { id: 20, name: 'Identity 250ml', category: CATEGORIES.BRANDY, price: 220, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Identity Brandy 250ml' },
  { id: 21, name: 'Napps', category: CATEGORIES.BRANDY, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Napps Brandy' },

  // Spirit (12 products)
  { id: 22, name: 'KC 750ml', category: CATEGORIES.SPIRIT, price: 1150, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Kenya Cane 750ml' },
  { id: 23, name: 'KC 250ml', category: CATEGORIES.SPIRIT, price: 400, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Kenya Cane 250ml' },
  { id: 24, name: 'Konyagi 750ml', category: CATEGORIES.SPIRIT, price: 1000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Konyagi 750ml' },
  { id: 25, name: 'Konyagi 250ml', category: CATEGORIES.SPIRIT, price: 350, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Konyagi 250ml' },
  { id: 26, name: 'Chrome 750ml', category: CATEGORIES.SPIRIT, price: 900, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Chrome Spirit 750ml' },
  { id: 27, name: 'Chrome 250ml', category: CATEGORIES.SPIRIT, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Chrome Spirit 250ml' },
  { id: 28, name: 'K. Extra 750ml', category: CATEGORIES.SPIRIT, price: 900, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'K. Extra Spirit 750ml' },
  { id: 29, name: 'K. Extra 250ml', category: CATEGORIES.SPIRIT, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'K. Extra Spirit 250ml' },
  { id: 30, name: 'GM 750ml', category: CATEGORIES.SPIRIT, price: 1000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'GM Spirit 750ml' },
  { id: 31, name: 'GM 250ml', category: CATEGORIES.SPIRIT, price: 340, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'GM Spirit 250ml' },
  { id: 32, name: 'K.K 750ml', category: CATEGORIES.SPIRIT, price: 950, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'K.K Spirit 750ml' },
  { id: 33, name: 'K.K 250ml', category: CATEGORIES.SPIRIT, price: 300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'K.K Spirit 250ml' },
  { id: 34, name: 'Orijin 750ml', category: CATEGORIES.SPIRIT, price: 1000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Orijin Drink 750ml' },
  { id: 35, name: 'Orijin 250ml', category: CATEGORIES.SPIRIT, price: 450, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Orijin Drink 250ml' },

  // Gin (8 products)
  { id: 36, name: 'Gilbeys 750ml', category: CATEGORIES.GIN, price: 1700, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Gilbey\'s Gin 750ml' },
  { id: 37, name: 'Gilbeys 250ml', category: CATEGORIES.GIN, price: 600, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Gilbey\'s Gin 250ml' },
  { id: 38, name: 'Gordons 750ml', category: CATEGORIES.GIN, price: 2500, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Gordon\'s Gin 750ml' },
  { id: 39, name: 'Gordons 1l', category: CATEGORIES.GIN, price: 3300, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Gordon\'s Gin 1L' },
  { id: 40, name: 'Tanqueray 750ml', category: CATEGORIES.GIN, price: 4000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Tanqueray Gin 750ml' },
  { id: 41, name: 'Caribia 750ml', category: CATEGORIES.GIN, price: 1100, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Caribia Gin 750ml' },
  { id: 42, name: 'Caribia 250ml', category: CATEGORIES.GIN, price: 400, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Caribia Gin 250ml' },
  { id: 43, name: 'Best Gin 250ml', category: CATEGORIES.GIN, price: 450, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Best Gin 250ml' },

  // Whiskey (6 products)
  { id: 44, name: 'Hunters 750ml', category: CATEGORIES.WHISKEY, price: 1150, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Hunters Choice Whisky 750ml' },
  { id: 45, name: 'Hunters 250ml', category: CATEGORIES.WHISKEY, price: 450, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Hunters Choice Whisky 250ml' },
  { id: 46, name: 'B & White 750ml', category: CATEGORIES.WHISKEY, price: 1600, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Black and White Whisky 750ml' },
  { id: 47, name: 'Jagermister 750ml', category: CATEGORIES.WHISKEY, price: 4000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Jagermeister 750ml' },
  { id: 48, name: 'Red Label 750ml', category: CATEGORIES.WHISKEY, price: 2500, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Johnnie Walker Red Label 750ml' },
  { id: 49, name: 'JD Black Label 750ml', category: CATEGORIES.WHISKEY, price: 3500, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Jack Daniels Black Label 750ml' },

  // Tequila (1 product)
  { id: 50, name: 'Camino', category: CATEGORIES.TEQUILA, price: 4000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Camino Tequila' },

  // Liquer (4 products)
  { id: 51, name: 'V&A 750ml', category: CATEGORIES.LIQUER, price: 1200, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'V&A Cream Liqueur 750ml' },
  { id: 52, name: 'V&A 250ml', category: CATEGORIES.LIQUER, price: 450, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'V&A Cream Liqueur 250ml' },
  { id: 53, name: 'Lemon Punch 750ml', category: CATEGORIES.LIQUER, price: 850, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Lemon Punch Liqueur 750ml' },
  { id: 54, name: 'Lemon Punch 250ml', category: CATEGORIES.LIQUER, price: 260, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Lemon Punch Liqueur 250ml' },

  // Wine (2 products)
  { id: 55, name: 'Caprice', category: CATEGORIES.WINE, price: 1000, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Caprice Wine' },
  { id: 56, name: '4th Street', category: CATEGORIES.WINE, price: 1200, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: '4th Street Wine' },

  // Soft Drinks (8 products)
  { id: 57, name: 'Delmonte', category: CATEGORIES.SOFT_DRINKS, price: 350, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Del Monte Juice' },
  { id: 58, name: 'Predator', category: CATEGORIES.SOFT_DRINKS, price: 70, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Predator Energy Drink' },
  { id: 59, name: 'Lemonade', category: CATEGORIES.SOFT_DRINKS, price: 60, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Lemonade Drink' },
  { id: 60, name: 'Water Big', category: CATEGORIES.SOFT_DRINKS, price: 100, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Bottled Water 1L' },
  { id: 61, name: 'Water Small', category: CATEGORIES.SOFT_DRINKS, price: 50, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Bottled Water 500ml' },
  { id: 62, name: 'Soda Big', category: CATEGORIES.SOFT_DRINKS, price: 100, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Soda Drink 1L' },
  { id: 63, name: 'Soda Small', category: CATEGORIES.SOFT_DRINKS, price: 60, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: 'Soda Drink 300ml' },
  { id: 64, name: '3 Bob', category: CATEGORIES.SOFT_DRINKS, price: 1, image: 'https://i.etsystatic.com/15102934/r/il/9128ab/2267994222/il_570xN.2267994222_kjbb.jpg', description: '3 Bob Energy Drink' },
];

/**
 * Get all products
 * @returns {Array} Array of all products
 */
function getAllProducts() {
  return PRODUCTS;
}

/**
 * Get products by category
 * @param {string} category - Category name
 * @returns {Array} Filtered products
 */
function getProductsByCategory(category) {
  if (category === 'All' || category === CATEGORIES.TRENDING) {
    return PRODUCTS;
  }
  return PRODUCTS.filter(product => product.category === category);
}

/**
 * Get product by ID
 * @param {number} id - Product ID
 * @returns {Object} Product object
 */
function getProductById(id) {
  return PRODUCTS.find(product => product.id === id);
}

/**
 * Search products by name
 * @param {string} query - Search query
 * @returns {Array} Filtered products
 */
function searchProducts(query) {
  const searchTerm = query.toLowerCase();
  return PRODUCTS.filter(product =>
    product.name.toLowerCase().includes(searchTerm) ||
    product.category.toLowerCase().includes(searchTerm) ||
    product.description.toLowerCase().includes(searchTerm)
  );
}

/**
 * Format price in KES currency
 * @param {number} amount - Amount to format
 * @returns {string} Formatted price
 */
function formatPrice(amount) {
  return `KES ${amount.toLocaleString()}`;
}

/**
 * Get all available categories
 * @returns {Array} Array of category names
 */
function getCategories() {
  const uniqueCategories = [...new Set(PRODUCTS.map(p => p.category))];
  return ['All', ...uniqueCategories.sort()];
}

/**
 * Format WhatsApp message for product inquiry
 * @param {Object} product - Product object
 * @returns {string} Formatted message
 */
function formatWhatsAppProductMessage(product) {
  return `Hi, I'm interested in ${product.name} priced at ${formatPrice(product.price)}. Can you tell me more?`;
}

/**
 * Format WhatsApp message for order
 * @param {Array} cartItems - Items in cart
 * @param {number} total - Total amount
 * @param {Object} customer - Customer details
 * @param {string} paymentRef - Payment reference
 * @returns {string} Formatted order message
 */
function formatWhatsAppOrderMessage(cartItems, total, customer, paymentRef) {
  let message = `*Order Confirmation*\n\n`;
  message += `Hello ${customer.name},\n\n`;
  message += `Thank you for your order from ${CONFIG.businessName}!\n\n`;
  message += `*Order Items:*\n`;

  cartItems.forEach(item => {
    const product = getProductById(item.productId);
    message += `• ${product.name} x${item.quantity} = ${formatPrice(product.price * item.quantity)}\n`;
  });

  message += `\n*Order Total:* ${formatPrice(total)}\n`;
  message += `*Payment Details (Name & Time):* ${paymentRef}\n`;
  message += `*Email:* ${customer.email}\n`;
  message += `*Phone:* ${customer.phone}\n`;
  message += `*Delivery Location:* ${customer.deliveryLocation}\n\n`;
  message += `Thank you for shopping with us!`;

  return message;
}
