// card-catalog.jsx
// ─────────────────────────────────────────────────────────────────────────────
// CARD CATALOG — single source of truth for the "tick what cards you have"
// onboarding. Each entry mirrors the `cards` table columns (snake_case) so a
// selected card can be written to the active backend with almost no translation:
//
//     const { _meta, ...row } = entry;
//     await insert({ ...row, user_id, ...collectedFields });
//
// Personal fields are intentionally absent here:
//   - Dropped entirely: user_id, last4, statement_balance, due_date,
//     chosen_category_history, prvi_promo_credited_at, archived, created_at
//   - Collected at onboarding (see _meta.collect): chosen_category_id,
//     membership_year_start, statement_close_day
//
// This file is purely additive. Nothing loads it yet, so adding it to the repo
// cannot affect the existing dashboard. It becomes live only once the onboarding
// screen + seed function are wired in.
// ─────────────────────────────────────────────────────────────────────────────

window.CARD_CATALOG = [
  // ─── Credit · Miles ────────────────────────────────────────────────────────
  {
    id: "uob-pp",
    name: "UOB Preferred Platinum",
    color: "#0D47A1",
    accent: "#bfa181",
    network: "Visa",
    type: "credit",
    bonus_kind: "ppv",
    bonus_rate: 4,
    bonus_base_rate: 0.4,
    bonus_cap_sgd: 1200,
    bonus_period: "monthly",
    bonus_category_label: "Online (S$600) + Contactless (S$600)",
    contactless_cap_sgd: 600,
    online_cap_sgd: 600,
    miles_conversion_rate: 2,
    reward_unit: "UNI$",
    _meta: {
      group: "credit",
      issuer: "UOB",
      description: "4 mpd on online + contactless spend, each capped at S$600/mo.",
      collect: [],
    },
  },
  {
    id: "uob-prvi",
    name: "UOB PRVI Miles",
    color: "#7C3AED",
    accent: "#c9a961",
    network: "Visa",
    type: "credit",
    bonus_kind: "prvi",
    bonus_rate: 3,
    bonus_base_rate: 1.4,
    bonus_cap_sgd: null,
    bonus_period: "monthly",
    bonus_category_label: "Local 1.4 · SEA 3.0 · Other O/S 2.4 · Agoda/Expedia up to 8",
    sea_rate: 3,
    other_overseas_rate: 2.4,
    hotel_booking_rate: 8,
    miles_conversion_rate: 2,
    reward_unit: "UNI$",
    _meta: {
      group: "credit",
      issuer: "UOB",
      description: "1.4 mpd local, 3.0 mpd SEA, 2.4 mpd other overseas, up to 8 on hotel bookings.",
      collect: [],
    },
  },
  {
    id: "uob-krisflyer",
    name: "UOB KrisFlyer",
    color: "#0D9488",
    accent: "#d4af37",
    network: "Visa",
    type: "credit",
    bonus_kind: "krisflyer",
    bonus_rate: 3,
    bonus_base_rate: 1.2,
    bonus_cap_sgd: null,
    bonus_period: "membership-year",
    bonus_category_label: "SQ-related = 3mpd · Accelerated = 1.2/2.4mpd · Other = 1.2mpd",
    sq_gate_sgd: 1000,
    accelerated_rate: 2.4,
    miles_conversion_rate: null, // earns miles directly — no conversion step
    reward_unit: "miles",
    _meta: {
      group: "credit",
      issuer: "UOB",
      description: "3 mpd on SQ/KrisFlyer spend, up to 2.4 mpd accelerated, 1.2 mpd otherwise. Needs a S$1,000 gate.",
      // Each cardholder's membership year starts on their own anniversary.
      collect: ["membership_year_start"],
    },
  },
  {
    id: "uob-ladys",
    name: "UOB Lady's",
    color: "#E91E63",
    accent: "#e91e63",
    network: "Visa",
    type: "credit",
    bonus_kind: "ladys",
    bonus_rate: 4,
    bonus_base_rate: 0.4,
    bonus_cap_sgd: 1000,
    bonus_period: "monthly",
    bonus_category_label: "Chosen category (quarterly)",
    miles_conversion_rate: 2,
    reward_unit: "UNI$",
    _meta: {
      group: "credit",
      issuer: "UOB",
      description: "4 mpd on one self-chosen category, re-selectable each quarter.",
      // User picks their own bonus category; quarter anchor is auto-computed.
      collect: ["chosen_category_id"],
    },
  },
  {
    id: "citi-rewards",
    name: "Citi Rewards",
    color: "#0066CC",
    accent: "#dc2626",
    network: "Mastercard",
    type: "credit",
    bonus_kind: "citi-rewards",
    bonus_rate: 4,
    bonus_base_rate: 0.4,
    bonus_cap_sgd: 1000,
    bonus_period: "monthly",
    bonus_category_label: "Online (Gojek/Grab/Shopee/etc.)",
    miles_conversion_rate: 0.4,
    reward_unit: "points",
    _meta: {
      group: "credit",
      issuer: "Citi",
      description: "4 mpd on online/shopping spend, capped at S$1,000/mo.",
      // Citi statement cycle differs per cardholder.
      collect: ["statement_close_day"],
    },
  },

  // ─── Credit · Cashback ───────────────────────────────────────────────────────
  {
    id: "maribank",
    name: "Maribank",
    color: "#84CC16",
    accent: "#22d3ee",
    network: "Mastercard",
    type: "credit",
    bonus_kind: "cashback-overseas",
    bonus_rate: 1.5,
    bonus_base_rate: 0,
    bonus_cap_sgd: 1500,
    bonus_period: "monthly",
    bonus_category_label: "1.5% cashback on overseas spend (cap S$1,500/mo)",
    _meta: {
      group: "credit",
      issuer: "Maribank",
      description: "1.5% cashback on overseas spend, capped at S$1,500/mo.",
      collect: [],
    },
  },
  {
    id: "trust",
    name: "Trust",
    color: "#FF4D00",
    accent: "#a78bfa",
    network: "Mastercard",
    type: "credit",
    bonus_kind: "cashback-tiered",
    bonus_rate: 1,
    bonus_base_rate: 0,
    bonus_cap_sgd: null,
    bonus_period: "monthly",
    bonus_category_label: "Cashback (1% local · 0.5% overseas)",
    bonus_overseas_rate: 0.5,
    _meta: {
      group: "credit",
      issuer: "Trust",
      description: "1% cashback local, 0.5% overseas.",
      collect: [],
    },
  },

  // ─── Debit (no rewards engine) ───────────────────────────────────────────────
  {
    id: "dbs-savings",
    name: "DBS Savings",
    color: "#DC2626",
    accent: "#fca5a5",
    network: "Visa",
    type: "debit",
    bonus_kind: null,
    _meta: { group: "debit", issuer: "DBS", description: "DBS savings debit card.", collect: [] },
  },
  {
    id: "dbs-mc",
    name: "DBS Multi-Currency",
    color: "#F59E0B",
    accent: "#fcd34d",
    network: "Mastercard",
    type: "debit",
    bonus_kind: null,
    _meta: { group: "debit", issuer: "DBS", description: "DBS multi-currency debit card.", collect: [] },
  },
  {
    id: "uob-one",
    name: "UOB One Debit",
    color: "#06B6D4",
    accent: "#60a5fa",
    network: "Visa",
    type: "debit",
    bonus_kind: null,
    _meta: { group: "debit", issuer: "UOB", description: "UOB One debit card.", collect: [] },
  },
  {
    id: "choc-sgd",
    name: "Chocolate (SGD)",
    color: "#A16207",
    accent: "#a16207",
    network: "Visa",
    type: "debit",
    bonus_kind: null,
    _meta: { group: "debit", issuer: "Chocolate Finance", description: "Chocolate Finance SGD debit card.", collect: [] },
  },
  {
    id: "choc-usd",
    name: "Chocolate (USD)",
    color: "#525252",
    accent: "#92400e",
    network: "Visa",
    type: "debit",
    bonus_kind: null,
    _meta: { group: "debit", issuer: "Chocolate Finance", description: "Chocolate Finance USD debit card.", collect: [] },
  },
];

// ─── Seed-time transform ─────────────────────────────────────────────────────
// Turns a catalog entry into a row ready for `cards` insert. Strips the
// onboarding-only `_meta`, attaches the user, applies safe defaults, and lets
// onboarding pass collected per-user fields (chosen_category_id,
// membership_year_start, statement_close_day, last4, etc.) via `overrides`.
//
// Quarter / membership anchors that the user doesn't explicitly provide default
// to sensible "starts now" values so the bonus engines have something to work
// against from day one.
window.cardRowFromCatalog = function cardRowFromCatalog(entry, userId, overrides = {}) {
  const { _meta, ...row } = entry;

  const today = new Date();
  const firstOfThisMonth = new Date(today.getFullYear(), today.getMonth(), 1)
    .toISOString().slice(0, 10);
  const firstOfThisQuarter = new Date(today.getFullYear(), Math.floor(today.getMonth() / 3) * 3, 1)
    .toISOString().slice(0, 10);

  const defaults = {
    user_id: userId,
    last4: null,
    statement_balance: 0,
    archived: false,
  };

  if (row.bonus_kind === "ladys") {
    defaults.ladys_quarter_start = firstOfThisQuarter;
  }
  if (row.bonus_kind === "krisflyer") {
    defaults.membership_year_start = firstOfThisMonth;
  }

  // overrides win over defaults (e.g. a user-supplied membership anniversary)
  return { ...defaults, ...row, ...overrides };
};

window.getCatalogCard = function getCatalogCard(id) {
  return window.CARD_CATALOG.find(c => c.id === id) || null;
};
