// suzie-context.jsx
// Canonical context for the prototype's example user, Suzie Garcia,
// and her household. EVERY screen reads from this — never hard-code
// names, ages, debts, or income in individual screens.
//
// Loaded BEFORE everything else so subsequent scripts can read SUZIE.
//
// To change the example user, change this file and the prototype
// reflows automatically (sample policy text, payout facts, AI
// context, the lot).

const SUZIE = {
  user: {
    name: "Suzie Garcia",
    firstName: "Suzie",
    age: 43,
    occupation: "Director of Operations",
    employer: "Aurora Health Systems",
    incomeAnnual: 108000,
    location: "San Mateo, CA",
  },
  // Known people in Suzie's life — used to enrich beneficiary rows
  // (age, relationship hints) when the AI extracts a name from a
  // policy, and used by the payout flow to age-out milestones
  // (college, milestone gifts) appropriately.
  knownPeople: [
    {
      name: "John Garcia", firstName: "John", age: 47, relationship: "Spouse",
      bio: "Husband, marketing director, plays in a weekend soccer league.",
      incomeAnnual: 102000,
      // Years from "now" to common milestones for this person; null = N/A.
      retirementInYears: 18,
    },
    {
      name: "Sarah Garcia", firstName: "Sarah", age: 15, relationship: "Daughter",
      bio: "Sophomore, college-bound, loves travel and theatre.",
      collegeInYears: 3,    // starts college in ~3 years
      gradInYears: 7,       // grad school window
      milestoneInYears: 15, // wedding / launch window
    },
  ],
  household: {
    // Debts (Plaid-synced)
    mortgageBalance: 621000,
    mortgageYearsLeft: 24,
    mortgageMonthly: 3680,
    mortgageBank: "Chase",
    autoLoan: 23000,
    autoVehicle: "2023 Toyota RAV4",
    autoBank: "Bank of America",
    creditCard: 18560,
    studentLoans: 0,
    // Spending + savings
    annualSpend: 132000,
    monthlySpend: 11000,
    savings: 54000,
    // Income (combined)
    suzieIncome: 108000,
    spouseIncome: 102000,
    jointIncome: 210000,
  },
};

// Resolve a person row (whatever the beneficiary screen produced)
// against the known-people directory, filling in missing details.
// Non-empty user-entered fields take precedence over the directory;
// empty/missing user fields fall through to what we already know.
function suzieEnrich(personRow) {
  if (!personRow) return null;
  const known = SUZIE.knownPeople.find(k => k.name === personRow.name);
  if (!known) return { ...personRow };
  const merged = { ...known };
  Object.keys(personRow).forEach(k => {
    const v = personRow[k];
    if (v != null && v !== "") merged[k] = v;
  });
  return merged;
}

// Infer the kind of beneficiary for payout-expense templating:
// "spouse" | "child" | "parent" | "other"
function suzieKindOf(relationship) {
  const r = (relationship || "").toLowerCase();
  if (/(spouse|partner|husband|wife)/.test(r)) return "spouse";
  if (/(son|daughter|child|grand)/.test(r)) return "child";
  if (/(mother|father|parent)/.test(r)) return "parent";
  return "other";
}

// Years-until milestone for an inferred person. Uses known-people data
// if name matches, otherwise sensible defaults from age.
function suzieMilestonesFor(personRow) {
  const k = suzieEnrich(personRow);
  const age = k?.age || null;
  const kind = suzieKindOf(personRow.relationship);
  if (kind === "child") {
    return {
      collegeInYears:   k?.collegeInYears   ?? (age != null ? Math.max(0, 18 - age) : 6),
      gradInYears:      k?.gradInYears      ?? (age != null ? Math.max(2, 22 - age) : 10),
      milestoneInYears: k?.milestoneInYears ?? (age != null ? Math.max(5, 28 - age) : 14),
    };
  }
  if (kind === "spouse") {
    return {
      retirementInYears: k?.retirementInYears ?? (age != null ? Math.max(2, 65 - age) : 20),
    };
  }
  return {};
}

window.SUZIE = SUZIE;
window.suzieEnrich = suzieEnrich;
window.suzieKindOf = suzieKindOf;
window.suzieMilestonesFor = suzieMilestonesFor;
