// data.jsx — Shared data for CheapCarrots
// Stores, products, delivery partners

const STORES = [
  {
    id: 'greenleaf',
    name: 'Greenleaf',
    tagline: 'Fresh & Local',
    color: '#2D7A4F',
    bg: '#E8F2EA',
    swatch: '🥬',
    qualityAvg: 88,
    rating: 4.7,
    reviews: 12483,
    distance: '0.8 mi',
    pickupReady: '15 min',
    tags: ['organic', 'local'],
  },
  {
    id: 'pantryco',
    name: 'Pantry Co',
    tagline: 'Your everyday market',
    color: '#D4582A',
    bg: '#FBE9DC',
    swatch: '🥕',
    qualityAvg: 76,
    rating: 4.4,
    reviews: 28910,
    distance: '1.2 mi',
    pickupReady: '20 min',
    tags: ['everyday', 'wide selection'],
  },
  {
    id: 'bulkmart',
    name: 'BulkMart',
    tagline: 'Save by the case',
    color: '#3A5BA0',
    bg: '#E2E8F4',
    swatch: '📦',
    qualityAvg: 68,
    rating: 4.2,
    reviews: 45120,
    distance: '3.4 mi',
    pickupReady: '45 min',
    tags: ['bulk', 'lowest price'],
  },
  {
    id: 'fieldfern',
    name: 'Field & Fern',
    tagline: 'Farm-direct, premium',
    color: '#6B3FA0',
    bg: '#ECE3F4',
    swatch: '🌿',
    qualityAvg: 94,
    rating: 4.9,
    reviews: 4271,
    distance: '2.1 mi',
    pickupReady: '30 min',
    tags: ['premium', 'farm-direct'],
  },
  {
    id: 'cornercrate',
    name: 'Corner Crate',
    tagline: 'Right around the block',
    color: '#C9A227',
    bg: '#F6EFD2',
    swatch: '🍞',
    qualityAvg: 72,
    rating: 4.3,
    reviews: 8912,
    distance: '0.3 mi',
    pickupReady: '10 min',
    tags: ['neighborhood', 'fast'],
  },
];

const DELIVERY_PARTNERS = [
  { id: 'dash', name: 'Dash', color: '#E63946', etaMin: 35, etaMax: 50, fee: 4.99 },
  { id: 'swift', name: 'Swift', color: '#0A7C8C', etaMin: 25, etaMax: 40, fee: 5.99 },
  { id: 'hop',   name: 'Hop',   color: '#7A4FBF', etaMin: 45, etaMax: 70, fee: 2.99 },
];

// Per-item pricing across stores. Quality is the LAB-style 0-100 score.
const PRODUCTS = [
  {
    id: 'carrots',
    name: 'Organic Carrots',
    unit: '1 lb bunch',
    category: 'produce',
    emoji: '🥕',
    prices: {
      greenleaf:   { price: 3.49, quality: 92, freshness: 95, organic: true,  tag: 'Picked yesterday' },
      pantryco:    { price: 2.79, quality: 78, freshness: 80, organic: false, tag: 'Crowd favorite' },
      bulkmart:    { price: 1.99, quality: 64, freshness: 65, organic: false, tag: 'Cheapest' },
      fieldfern:   { price: 4.29, quality: 98, freshness: 99, organic: true,  tag: 'Farm-direct' },
      cornercrate: { price: 3.19, quality: 74, freshness: 76, organic: false, tag: 'Just down the block' },
    },
  },
  {
    id: 'milk',
    name: 'Whole Milk',
    unit: '1 gallon',
    category: 'dairy',
    emoji: '🥛',
    prices: {
      greenleaf:   { price: 5.49, quality: 90, organic: true,  tag: 'Grass-fed' },
      pantryco:    { price: 3.99, quality: 76, organic: false, tag: 'Family-size' },
      bulkmart:    { price: 3.49, quality: 70, organic: false, tag: 'Best value' },
      fieldfern:   { price: 6.99, quality: 96, organic: true,  tag: 'Small-farm' },
      cornercrate: { price: 4.79, quality: 74, organic: false, tag: 'Stocked daily' },
    },
  },
  {
    id: 'bread',
    name: 'Sourdough Loaf',
    unit: '1 lb',
    category: 'bakery',
    emoji: '🍞',
    prices: {
      greenleaf:   { price: 5.99, quality: 86, tag: 'House bakery' },
      pantryco:    { price: 4.49, quality: 72, tag: 'Bake-off-site' },
      bulkmart:    { price: 3.29, quality: 60, tag: 'Pre-packaged' },
      fieldfern:   { price: 7.49, quality: 95, tag: 'Wood-fired' },
      cornercrate: { price: 5.49, quality: 80, tag: 'Baked at 5am' },
    },
  },
  {
    id: 'eggs',
    name: 'Eggs',
    unit: 'dozen, large',
    category: 'dairy',
    emoji: '🥚',
    prices: {
      greenleaf:   { price: 6.49, quality: 88, organic: true,  tag: 'Pasture-raised' },
      pantryco:    { price: 4.79, quality: 74, organic: false, tag: 'Cage-free' },
      bulkmart:    { price: 3.99, quality: 65, organic: false, tag: 'Conventional' },
      fieldfern:   { price: 8.99, quality: 97, organic: true,  tag: 'From Maple Hill Farm' },
      cornercrate: { price: 5.99, quality: 78, organic: false, tag: 'Local' },
    },
  },
  {
    id: 'apples',
    name: 'Honeycrisp Apples',
    unit: '3 lb bag',
    category: 'produce',
    emoji: '🍎',
    prices: {
      greenleaf:   { price: 7.99, quality: 90, freshness: 92, organic: true,  tag: 'Crispy & sweet' },
      pantryco:    { price: 6.49, quality: 78, freshness: 78, organic: false, tag: 'Lunchbox-ready' },
      bulkmart:    { price: 5.49, quality: 65, freshness: 60, organic: false, tag: 'Cold-stored' },
      fieldfern:   { price: 9.49, quality: 96, freshness: 98, organic: true,  tag: 'Tree-ripened' },
      cornercrate: { price: 7.29, quality: 80, freshness: 75, organic: false, tag: 'Pick-of-the-week' },
    },
  },
  {
    id: 'pasta',
    name: 'Bronze-cut Pasta',
    unit: '1 lb',
    category: 'pantry',
    emoji: '🍝',
    prices: {
      greenleaf:   { price: 4.99, quality: 86, tag: 'Italian import' },
      pantryco:    { price: 2.49, quality: 70, tag: 'Pantry staple' },
      bulkmart:    { price: 1.79, quality: 58, tag: 'Bulk box' },
      fieldfern:   { price: 6.49, quality: 94, tag: 'Artisan, slow-dried' },
      cornercrate: { price: 3.99, quality: 76, tag: 'Italian-American' },
    },
  },
  {
    id: 'tomato',
    name: 'Tomato Sauce',
    unit: '24 oz jar',
    category: 'pantry',
    emoji: '🥫',
    prices: {
      greenleaf:   { price: 6.49, quality: 88, organic: true, tag: 'Vine-ripened' },
      pantryco:    { price: 3.99, quality: 74, tag: 'Family recipe' },
      bulkmart:    { price: 2.99, quality: 62, tag: 'Big jar' },
      fieldfern:   { price: 8.99, quality: 95, organic: true, tag: 'Heirloom blend' },
      cornercrate: { price: 5.49, quality: 78, tag: 'Garlic-forward' },
    },
  },
  {
    id: 'spinach',
    name: 'Baby Spinach',
    unit: '5 oz clamshell',
    category: 'produce',
    emoji: '🥬',
    prices: {
      greenleaf:   { price: 4.49, quality: 91, freshness: 93, organic: true, tag: 'Triple-washed' },
      pantryco:    { price: 3.49, quality: 76, freshness: 78, tag: 'Family pack' },
      bulkmart:    { price: 2.79, quality: 62, freshness: 60, tag: 'Bulk' },
      fieldfern:   { price: 5.49, quality: 95, freshness: 97, organic: true, tag: 'Same-day-harvest' },
      cornercrate: { price: 4.19, quality: 78, freshness: 74, tag: 'Local' },
    },
  },
];

// Default cart used in cart screen
const DEFAULT_CART = [
  { productId: 'carrots',  qty: 1 },
  { productId: 'milk',     qty: 1 },
  { productId: 'eggs',     qty: 2 },
  { productId: 'apples',   qty: 1 },
  { productId: 'spinach',  qty: 1 },
  { productId: 'bread',    qty: 1 },
  { productId: 'pasta',    qty: 2 },
];

const CATEGORIES = [
  { id: 'all',     label: 'All',      emoji: '🛒' },
  { id: 'produce', label: 'Produce',  emoji: '🥬' },
  { id: 'dairy',   label: 'Dairy',    emoji: '🥛' },
  { id: 'bakery',  label: 'Bakery',   emoji: '🍞' },
  { id: 'pantry',  label: 'Pantry',   emoji: '🥫' },
];

const DEALS = [
  { id: 'd1', store: 'greenleaf',   product: 'Honeycrisp Apples', emoji: '🍎', was: 9.99, now: 7.99, badge: '20% off' },
  { id: 'd2', store: 'pantryco',    product: 'Whole Milk',         emoji: '🥛', was: 4.99, now: 3.99, badge: 'Mfr coupon' },
  { id: 'd3', store: 'bulkmart',    product: 'Pasta 6-pack',       emoji: '🍝', was: 12.99, now: 8.99, badge: 'Bulk save' },
  { id: 'd4', store: 'fieldfern',   product: 'Heirloom Tomatoes',  emoji: '🍅', was: 11.49, now: 8.99, badge: 'Harvest week' },
  { id: 'd5', store: 'cornercrate', product: 'Sourdough Loaf',     emoji: '🍞', was: 6.49, now: 4.99, badge: 'Daily bake' },
];

// Helper: compute total of cart at a given store, with optional delivery
function storeTotal(cart, storeId, { delivery = null, includeFees = true, qualityWeight = 0.5 } = {}) {
  let subtotal = 0;
  let qualitySum = 0;
  let qualityWeightSum = 0;
  let availableCount = 0;
  let totalCount = 0;
  for (const line of cart) {
    const p = PRODUCTS.find(x => x.id === line.productId);
    if (!p) continue;
    totalCount++;
    const offer = p.prices[storeId];
    if (!offer) continue;
    availableCount++;
    subtotal += offer.price * line.qty;
    qualitySum += offer.quality * line.qty;
    qualityWeightSum += line.qty;
  }
  const fees = delivery ? delivery.fee : 0;
  const serviceFee = delivery ? subtotal * 0.08 : 0;
  const total = subtotal + (includeFees ? fees + serviceFee : 0);
  const avgQuality = qualityWeightSum ? qualitySum / qualityWeightSum : 0;
  // value score: lower price + higher quality scaled by qualityWeight
  // returns 0-100; higher is better
  return {
    subtotal,
    fees,
    serviceFee,
    total,
    avgQuality,
    availableCount,
    totalCount,
    completeness: totalCount ? availableCount / totalCount : 0,
  };
}

Object.assign(window, {
  STORES, DELIVERY_PARTNERS, PRODUCTS, DEFAULT_CART, CATEGORIES, DEALS, storeTotal,
});
