// Shared mock data for Extra Dollar iOS prototype.

const ED_CARDS = [
  {
    id: "csp",
    issuer: "Chase",
    name: "Sapphire Preferred",
    short: "Sapphire",
    last4: "4421",
    color: "#0a3a6e",
    accent: "#a4c8f5",
    glyph: "S",
    pointCurrency: "Ultimate Rewards",
    pointValue: 1.8,
    annualFee: 95,
    rates: [
      { cat: "Dining", x: 3, note: "incl. delivery" },
      { cat: "Travel", x: 5, note: "via Chase portal" },
      { cat: "Streaming", x: 3 },
      { cat: "Online groceries", x: 3 },
      { cat: "Everything else", x: 1 },
    ],
  },
  {
    id: "amex-gold",
    issuer: "Amex",
    name: "Gold",
    short: "Gold",
    last4: "1009",
    color: "#caa15a",
    accent: "#fff3d6",
    glyph: "G",
    pointCurrency: "Membership Rewards",
    pointValue: 2.0,
    annualFee: 325,
    rates: [
      { cat: "Dining", x: 4, note: "worldwide" },
      { cat: "Groceries", x: 4, note: "U.S. supermarkets, $25k cap" },
      { cat: "Flights", x: 3, note: "booked direct" },
      { cat: "Everything else", x: 1 },
    ],
  },
  {
    id: "citi-cust",
    issuer: "Citi",
    name: "Custom Cash",
    short: "Custom Cash",
    last4: "8830",
    color: "#1d3a8a",
    accent: "#dde7ff",
    glyph: "C",
    pointCurrency: "Cashback",
    pointValue: 1.0,
    annualFee: 0,
    rates: [
      { cat: "Top category", x: 5, note: "$500/mo cap, auto-picked" },
      { cat: "Everything else", x: 1 },
    ],
  },
  {
    id: "discover-it",
    issuer: "Discover",
    name: "it Cash Back",
    short: "Discover it",
    last4: "3017",
    color: "#e16b1b",
    accent: "#ffd9b8",
    glyph: "D",
    pointCurrency: "Cashback",
    pointValue: 1.0,
    annualFee: 0,
    rates: [
      { cat: "Rotating Q2", x: 5, note: "Gas + Wholesale Clubs" },
      { cat: "Everything else", x: 1 },
    ],
  },
];

// Returns ranked list of cards for a given category, with effective $ earned per $100.
function rankCards(category, amount = 100) {
  const ranked = ED_CARDS.map((c) => {
    let best = c.rates.find((r) => r.cat.toLowerCase() === category.toLowerCase());
    if (!best) best = c.rates.find((r) => r.cat === "Everything else") || { x: 1 };
    const cashEquivPct = (best.x * c.pointValue) / 100; // c per $
    const earn = amount * cashEquivPct;
    return { card: c, rate: best, earn, multiplier: best.x };
  });
  ranked.sort((a, b) => b.earn - a.earn);
  return ranked;
}

const ED_TXNS = [
  { id: "t1", merchant: "Blue Bottle Coffee", category: "Dining", amount: 8.50, used: "discover-it", date: "Today, 9:14 AM", logo: "☕" },
  { id: "t2", merchant: "Whole Foods Market", category: "Groceries", amount: 84.32, used: "csp", date: "Yesterday, 6:42 PM", logo: "🌿" },
  { id: "t3", merchant: "Lyft", category: "Travel", amount: 14.20, used: "discover-it", date: "Yesterday, 2:08 PM", logo: "L" },
  { id: "t4", merchant: "Sweetgreen", category: "Dining", amount: 18.75, used: "amex-gold", date: "Apr 26, 12:30 PM", logo: "S" },
  { id: "t5", merchant: "Shell", category: "Gas", amount: 52.18, used: "csp", date: "Apr 25, 8:11 AM", logo: "⛽" },
  { id: "t6", merchant: "United Airlines", category: "Flights", amount: 412.00, used: "csp", date: "Apr 23, 7:55 PM", logo: "U" },
  { id: "t7", merchant: "Trader Joe's", category: "Groceries", amount: 47.91, used: "discover-it", date: "Apr 22, 5:17 PM", logo: "T" },
  { id: "t8", merchant: "Spotify", category: "Streaming", amount: 11.99, used: "amex-gold", date: "Apr 22, 9:00 AM", logo: "♪" },
];

// Compute optimization for a transaction.
function optimize(txn) {
  const ranked = rankCards(txn.category, txn.amount);
  const optimal = ranked[0];
  const usedRanking = ranked.find((r) => r.card.id === txn.used) || ranked[ranked.length - 1];
  const missed = optimal.earn - usedRanking.earn;
  return {
    optimal: optimal.card,
    optimalEarn: optimal.earn,
    optimalX: optimal.multiplier,
    used: usedRanking.card,
    usedEarn: usedRanking.earn,
    usedX: usedRanking.multiplier,
    missed,
    isOptimal: missed < 0.01,
  };
}

const ED_BENEFITS = [
  { card: "amex-gold", name: "Uber Cash", value: 10, frequency: "monthly", used: 0, expires: "May 31", urgent: true },
  { card: "amex-gold", name: "Resy dining", value: 10, frequency: "monthly", used: 10, expires: "Apr 30", urgent: false, complete: true },
  { card: "csp", name: "DoorDash credit", value: 10, frequency: "monthly", used: 0, expires: "May 2", urgent: true },
  { card: "csp", name: "Lyft Pink trial", value: 0, frequency: "annual", used: 0, expires: "Sep 12", urgent: false, perk: true },
];

const ED_NEARBY = {
  merchant: "Trader Joe's",
  distance: "0.2 mi",
  address: "1185 Broadway, NY",
  category: "Groceries",
};

const ED_INSIGHTS = {
  thisMonth: { earned: 64.20, missed: 41.30 },
  ytd: { earned: 482.10, missed: 312.40 },
  topMissed: [
    { merchant: "Whole Foods Market", missed: 12.40, count: 3 },
    { merchant: "Shell", missed: 8.20, count: 2 },
    { merchant: "Sweetgreen", missed: 6.80, count: 4 },
  ],
};

// Notifications feed
const ED_NOTIFICATIONS = [
  { id: "n1", type: "nearby", merchant: "Trader Joe's", distance: "0.2 mi", card: "amex-gold", x: 4, time: "2m ago", unread: true, body: "You're near groceries. Use Amex Gold for 4× points (~$1.60 on a $20 spend)." },
  { id: "n2", type: "win", merchant: "Blue Bottle Coffee", earned: 0.43, card: "discover-it", time: "1h ago", unread: true, body: "Nice — Discover it earned 5% on your $8.50 dining spend. That's $0.43 in your pocket." },
  { id: "n3", type: "miss", merchant: "Whole Foods Market", missed: 2.53, suggested: "amex-gold", used: "csp", txn: { id: "t2" }, time: "1d ago", unread: false, body: "You used Sapphire on $84.32 of groceries. Amex Gold would've earned 4× — you missed $2.53." },
  { id: "n4", type: "benefit", title: "Uber Cash expires in 4 days", body: "Your $10 monthly Uber credit on Amex Gold expires May 31. Use it before it disappears.", time: "2d ago", unread: false },
  { id: "n5", type: "system", title: "Q3 categories are live", body: "Discover it now earns 5% on Restaurants and Drug Stores through September.", time: "1w ago", unread: false },
];

// Saved offers / merchant credits
const ED_OFFERS = [
  { id: "o1", merchant: "DoorDash", card: "csp", value: 10, expires: "May 2", desc: "$10 off any $20+ order", urgent: true },
  { id: "o2", merchant: "Uber", card: "amex-gold", value: 10, expires: "May 31", desc: "$10 monthly Uber Cash", urgent: true },
  { id: "o3", merchant: "Lyft Pink", card: "csp", value: 0, expires: "Sep 12", desc: "Free Pink membership trial", urgent: false },
  { id: "o4", merchant: "Resy", card: "amex-gold", value: 10, expires: "Apr 30", desc: "$10 dining credit", urgent: false, complete: true },
];

const ED_USER = {
  name: "Jordan Reyes",
  email: "jordan@reyes.io",
  initials: "JR",
  joined: "March 2026",
  plan: "Premium · Annual",
  notifSettings: {
    nearby: true,
    wins: true,
    misses: true,
    benefits: true,
    weekly: false,
  },
};

Object.assign(window, {
  ED_CARDS, ED_TXNS, ED_BENEFITS, ED_NEARBY, ED_INSIGHTS,
  ED_NOTIFICATIONS, ED_OFFERS, ED_USER,
  rankCards, optimize,
});
