export interface FMCGDetails {
  brandName: string;
  netWeight: string;
  isVeg: boolean;
  marketingClaim: string;
  ingredients: string;
  allergens: string;
  fssai: string;
  storage: string;
  barcode: string;
  batchNo: string;
}

function simpleHash(input: string): number {
  const str = String(input || '');
  let hash = 0;
  for (let i = 0; i < str.length; i++) {
    const char = str.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash; // Convert to 32bit integer
  }
  return Math.abs(hash);
}

const BRANDS = ["Nearle Organics", "Daily Fresh", "Harvest Select", "Nature's Best", "Prime Goods", "Evergreen Pantry"];
const CLAIMS_VEG = ["100% Natural", "Zero Trans Fat", "High in Fiber", "Rich in Protein", "Farm Fresh", "No Added Preservatives"];
const CLAIMS_NONVEG = ["High Protein", "Premium Cut", "Farm Raised", "Rich in Omega-3"];

const INGREDIENTS_BY_CAT: Record<string, string> = {
  'Staples': 'Whole Wheat, Refined Flour, Salt, Edible Vegetable Oil, Water.',
  'Groceries': 'Sugar, Spices, Salt, Citric Acid, Natural Flavors.',
  'Beverages': 'Purified Water, Sugar, Natural Fruit Extract, Citric Acid, Permitted Colors.',
  'Snacks': 'Potatoes, Edible Vegetable Oil, Salt, Spices & Condiments, Natural Flavors.',
  'Dairy': 'Pasteurized Cow Milk, Active Cultures, Milk Solids.',
  'Default': 'Wheat Flour, Sugar, Edible Veg Oil, Salt, Raising Agents, Natural Flavors.'
};

const ALLERGENS = [
  "Contains Wheat and Milk.",
  "Processed in a facility that handles soy.",
  "Contains Nuts and Dairy.",
  "May contain traces of Peanuts.",
  "None."
];

export function generateFMCGDetails(productId: string, category: string): FMCGDetails {
  const seed = simpleHash(productId);
  
  // Category fallback
  const catKey = Object.keys(INGREDIENTS_BY_CAT).find(k => String(category || '').includes(k)) || 'Default';
  
  const isVeg = (seed % 10) !== 0; // 90% vegetarian
  const claimsList = isVeg ? CLAIMS_VEG : CLAIMS_NONVEG;

  // Predictable outputs based on seed
  const brandName = BRANDS[seed % BRANDS.length];
  const weightOptions = ["150g", "250g", "500g", "1kg", "1L", "500ml", "75g", "200g"];
  const netWeight = weightOptions[seed % weightOptions.length];
  const marketingClaim = claimsList[(seed >> 1) % claimsList.length];
  const ingredients = INGREDIENTS_BY_CAT[catKey];
  const allergens = ALLERGENS[(seed >> 2) % ALLERGENS.length];
  
  // Fake FSSAI number (14 digits)
  const fssaiBase = "100" + (seed % 99) + "011000" + ((seed >> 3) % 999).toString().padStart(3, '0');
  const fssai = fssaiBase.padEnd(14, '0');

  const storageOptions = [
    "Store in a cool, dry place.",
    "Keep refrigerated after opening.",
    "Store away from direct sunlight.",
    "Transfer to an airtight container after opening."
  ];
  const storage = storageOptions[(seed >> 4) % storageOptions.length];

  // Barcode (EAN-13 style)
  const barcode = "890" + (seed % 9999999999).toString().padStart(10, '0');
  
  // Batch Number
  const batchNo = "BN-" + (seed % 9999).toString().padStart(4, '0') + String.fromCharCode(65 + (seed % 26));

  return {
    brandName,
    netWeight,
    isVeg,
    marketingClaim,
    ingredients,
    allergens,
    fssai,
    storage,
    barcode,
    batchNo
  };
}
