// Seed data + demo identity + offline intake fallback.
// Kept intentionally light: mock data is a feature (a self-explaining demo),
// not a shortcut. The live path replaces all of this with real Jira reads.

(function () {
  // The "signed-in" customer for the demo (single-tenant demo, no real auth).
  const ME = {
    name: "Priya Nair",
    email: "priya@acme.co",
    account: "Acme Co",
  };

  // Product areas the agent can tag a bug against.
  const COMPONENTS = [
    "Deal Rooms", "Forecast", "Addy (AI copilot)", "Slack integration",
    "Openings / Outbound", "Notifications", "Settings", "Other",
  ];

  // Seed a couple of tickets so both dashboards are populated on first open.
  // Only runs when the local mirror is empty.
  function seedIfEmpty() {
    if (!window.NS || !window.NS.tickets) return;
    if (window.NS.tickets.all().length) return;
    const now = Date.now();
    const seeds = [
      {
        key: "SUP-1042", url: null, live: false,
        reporter: "priya@acme.co", account: "Acme Co",
        title: "Forecast export drops the weighted column",
        component: "Forecast", severity: "SEV3",
        statusCategory: "In Progress", status: "Being worked on",
        createdAt: now - 1000 * 60 * 60 * 26, updatedAt: now - 1000 * 60 * 60 * 3,
        reproSteps: "Open Forecast with a custom view active, click Export, choose CSV.",
        expected: "The CSV includes the weighted-forecast column.",
        actual: "The weighted column is missing from the file.",
        environment: "Chrome 121 / macOS",
        blastRadius: "Just me, but I export weekly.",
        severityRationale: "Degraded export with a documented workaround; one user, not blocked.",
        kbVerdict: "known-issue",
        kbCitation: "Known issue: forecast CSV export can omit the weighted column",
      },
      {
        key: "SUP-1039", url: null, live: false,
        reporter: "priya@acme.co", account: "Acme Co",
        title: "Addy returns a blank answer on the Atlas deal",
        component: "Addy (AI copilot)", severity: "SEV2",
        statusCategory: "Done", status: "Resolved",
        createdAt: now - 1000 * 60 * 60 * 72, updatedAt: now - 1000 * 60 * 60 * 20,
        reproSteps: "Open the Atlas deal room, ask Addy 'what's the risk here?'",
        expected: "Addy answers with a read on the deal.",
        actual: "The response comes back empty.",
        environment: "Chrome / macOS",
        blastRadius: "Me and two others on the Atlas account.",
        severityRationale: "Core copilot broken on a live deal with no workaround.",
      },
      {
        key: "SUP-1051", url: null, live: false,
        reporter: "marcus@northwind.io", account: "Northwind",
        title: "Slack daily brief posted twice at 11:00",
        component: "Slack integration", severity: "SEV3",
        statusCategory: "To Do", status: "Logged",
        createdAt: now - 1000 * 60 * 90, updatedAt: now - 1000 * 60 * 90,
        reproSteps: "Reconnected Slack yesterday; this morning the 11:00 brief arrived twice.",
        expected: "One brief per morning.",
        actual: "Two identical briefs, a few seconds apart.",
        blastRadius: "Whole team sees the duplicate.",
        severityRationale: "Noisy duplicate, but the brief still works — reconnecting clears it.",
        kbVerdict: "known-issue",
        kbCitation: "Known issue: daily Slack brief posts twice at 11:00",
      },
    ];
    seeds.forEach((t) => window.NS.tickets.upsert(t));
  }

  // --- Offline fallback intake -------------------------------------------
  // Only used when the live LLM agent is unreachable (no ANTHROPIC_API_KEY).
  // Kept deliberately tiny so it never feels like a form: the first message IS
  // the report; ask ONE light question to gauge impact, then file. The first
  // message becomes both the title and the description.
  const OFFLINE_STEPS = [
    { key: "title", q: "" }, // first message — the greeting already asked
    { key: "blastRadius", q: "Got it — I'll get this logged. Quick one so I route it right: is this blocking you or your team, or more of a minor annoyance?" },
  ];

  // Guess severity from keywords when running offline. The live agent reasons
  // about it properly; this keeps the demo honest without a model call. Uses
  // word boundaries so e.g. "downloads" doesn't trip the "down" (outage) rule.
  function offlineSeverity(fields) {
    const blob = Object.values(fields).join(" ").toLowerCase();
    const has = (re) => re.test(blob);
    // SEV1: true outage / data loss / security / whole org down
    if (has(/\b(outage|offline|down for everyone|data ?loss|lost (my|our|the) data|security|breach|hacked|can'?t log ?in|cannot log ?in|nobody can|whole (company|org|company))\b/))
      return "SEV1";
    // SEV2: major feature broken / crash / whole team blocked (no easy workaround)
    if (has(/\b(crash\w*|broken|unusable|blocked|blocks|can'?t|cannot|no workaround|whole team|entire team|all of us)\b/))
      return "SEV2";
    // SEV4: cosmetic / minor
    if (has(/\b(cosmetic|typo|minor|slightly|alignment|spacing|wording|off by a pixel)\b/))
      return "SEV4";
    return "SEV3";
  }

  window.NS = Object.assign(window.NS || {}, {
    ME, COMPONENTS, seedIfEmpty,
    offline: { STEPS: OFFLINE_STEPS, severity: offlineSeverity },
  });
})();
