// Sample journey data — Mindworks AI Journey Builder

const ICONS = "assets/icons";

// Step kinds:
//   email     — sends an email
//   task      — creates a sales rep task
//   wait      — internal scheduling gap
//   trigger   — entry event
//   exit      — exit event
//
// Step statuses:
//   done      — already executed
//   skipped   — AI decided to skip
//   shifted   — AI rescheduled but will still run
//   pending   — upcoming, on schedule
//   active    — current step (in window)

const STEP_TEMPLATE = [
  { id: "trigger", kind: "trigger", day: 0,  title: "Quote Sent",                 detail: "Quote Q-982510 sent to Acme Manufacturing" },
  { id: "s1",      kind: "email",   day: 7,  title: "First Follow-Up",            detail: "Friendly check-in. Reference quote terms.", subject: "Following up on Q-982510" },
  { id: "s2",      kind: "email",   day: 21, title: "Second Follow-Up",           detail: "Add value: case study + pricing options.",  subject: "A few options on Q-982510" },
  { id: "s3",      kind: "task",    day: 60, title: "Sales Rep Task",             detail: "Assign call task to Sales Rep, with context dossier.", owner: "Owen Brooks" },
  { id: "s4",      kind: "email",   day: 90, title: "Final Check-In",             detail: "Last touch. Offers to close or shelve the quote.",       subject: "Closing the loop on Q-982510" },
  { id: "exit",    kind: "exit",    day: 90, title: "Journey End",                detail: "Mark journey complete." },
];

// The four journey states (selectable via tweaks)
const JOURNEY_STATES = {
  fresh: {
    label: "Just started",
    todayDay: 1,
    customer: { name: "Northern Forge & Tool", quote: "Q-981207", value: "$84,200", owner: "Owen Brooks", ownerInitials: "OB", ownerColor: "#76ADFF" },
    overrides: {
      trigger: { status: "done", at: "Yesterday, 4:12 pm" },
      s1: { status: "pending" },
      s2: { status: "pending" },
      s3: { status: "pending" },
      s4: { status: "pending" },
      exit: { status: "pending" },
    },
    decisions: [
      { day: 0, at: "Yesterday, 4:12 pm",  kind: "trigger", text: "Journey started — quote sent.", reason: "Trigger condition met: Quote Q-981207 status changed to Sent." },
    ],
  },
  midflight: {
    label: "Mid-flight (with skips)",
    todayDay: 34,
    customer: { name: "Acme Manufacturing", quote: "Q-982510", value: "$148,900", owner: "Owen Brooks", ownerInitials: "OB", ownerColor: "#76ADFF" },
    overrides: {
      trigger: { status: "done", at: "Mar 14, 9:02 am" },
      s1:      { status: "done", at: "Mar 21, 9:00 am", outcome: "Delivered • Opened 2x" },
      s2:      { status: "skipped", at: "Apr 4", reason: "Customer replied to first follow-up on Mar 23. AI suppressed redundant outreach." },
      s3:      { status: "shifted", from: 60, to: 67, reason: "Sales rep emailed customer on Apr 11. Task offset by 7 days." },
      s4:      { status: "pending" },
      exit:    { status: "pending" },
    },
    decisions: [
      { day: 0,  at: "Mar 14, 9:02 am", kind: "trigger", text: "Journey started — quote sent to Acme Manufacturing.", reason: "Trigger condition met: Quote Q-982510 status changed to Sent." },
      { day: 7,  at: "Mar 21, 9:00 am", kind: "sent",    text: "First Follow-Up email sent.", reason: "No customer reply detected in the last 7 days. Step executed on schedule." },
      { day: 9,  at: "Mar 23, 11:42 am", kind: "signal", text: "Inbound signal: Emma Connor replied to follow-up.", reason: "Email reply detected from emma@acmemfg.com. Flagged as active engagement." },
      { day: 21, at: "Apr 4",            kind: "skipped", text: "Skipped Second Follow-Up.", reason: "Customer replied 12 days ago. Rule: 'Suppress redundant outreach if reply within last 21 days.'" },
      { day: 28, at: "Apr 11, 2:18 pm",  kind: "signal", text: "Inbound signal: Sales rep sent manual email.", reason: "Manual outbound from Owen Brooks. Resetting downstream offsets." },
      { day: 28, at: "Apr 11, 2:18 pm",  kind: "shifted", text: "Sales Rep Task offset by 7 days (Day 60 → Day 67).", reason: "Rule: 'If sales rep manually contacted customer, offset next scheduled touchpoint.'" },
    ],
  },
  paused: {
    label: "Paused by user",
    todayDay: 22,
    customer: { name: "Cascade Hydraulics", quote: "Q-980441", value: "$32,100", owner: "Maya Iyer", ownerInitials: "MI", ownerColor: "#FF9143" },
    overrides: {
      trigger: { status: "done", at: "Apr 1, 10:11 am" },
      s1:      { status: "done", at: "Apr 8, 9:00 am", outcome: "Delivered" },
      s2:      { status: "pending", paused: true },
      s3:      { status: "pending", paused: true },
      s4:      { status: "pending", paused: true },
      exit:    { status: "pending", paused: true },
    },
    decisions: [
      { day: 0,  at: "Apr 1, 10:11 am", kind: "trigger", text: "Journey started.", reason: "Quote sent." },
      { day: 7,  at: "Apr 8, 9:00 am",  kind: "sent",    text: "First Follow-Up email sent.", reason: "No engagement detected." },
      { day: 22, at: "Apr 23, 11:00 am", kind: "paused", text: "Journey paused by Maya Iyer.", reason: "Manual pause. All upcoming steps suspended until resumed." },
    ],
  },
  ended: {
    label: "Completed — quote accepted",
    todayDay: 19,
    customer: { name: "Vector Steel Co.", quote: "Q-980022", value: "$211,400", owner: "Owen Brooks", ownerInitials: "OB", ownerColor: "#76ADFF" },
    overrides: {
      trigger: { status: "done", at: "Mar 30, 8:45 am" },
      s1:      { status: "done", at: "Apr 6, 9:00 am", outcome: "Delivered • Opened" },
      s2:      { status: "skipped", at: "Apr 18", reason: "Quote status changed to Accepted. Exit rule triggered." },
      s3:      { status: "skipped", at: "Apr 18", reason: "Journey ended early — quote accepted." },
      s4:      { status: "skipped", at: "Apr 18", reason: "Journey ended early — quote accepted." },
      exit:    { status: "done", at: "Apr 18, 3:14 pm", outcome: "Quote Accepted" },
    },
    decisions: [
      { day: 0,  at: "Mar 30, 8:45 am", kind: "trigger", text: "Journey started.", reason: "Quote sent." },
      { day: 7,  at: "Apr 6, 9:00 am",  kind: "sent",    text: "First Follow-Up email sent.", reason: "No engagement detected." },
      { day: 19, at: "Apr 18, 3:14 pm", kind: "exit",    text: "Journey ended — quote accepted.", reason: "Exit rule: 'If quote is accepted or rejected, automatically end the journey.'" },
    ],
  },
};

// Add bundling/consolidation decisions to the mid-flight journey
JOURNEY_STATES.midflight.decisions.splice(2, 0,
  { day: 7, at: "Mar 21, 9:00 am", kind: "bundled", text: "Bundled 3 quotes into one follow-up email.",
    reason: "Sales rep sent Q-982510, Q-982511, Q-982512 to Acme on the same day. AI consolidated outreach into a single message referencing all three quotes — instead of sending 3 separate follow-ups." }
);

// Templates — reusable automation definitions
const TEMPLATE_GROUPS = [
  { id: "sales",       name: "Sales Process" },
  { id: "billing",     name: "Customer Billing" },
  { id: "procurement", name: "Procurement" },
];

const TEMPLATES = [
  { id: "tpl-lead", group: "sales", name: "Lead Nurturing",
    description: "Warm new leads with paced touches until they're sales-ready.",
    trigger: "Lead.status = 'New'",
    steps: 4, conditions: 3, exits: 2,
    activeCount: 28, completedCount: 312, replyRate: 24, acceptRate: 11,
    owner: "Maya Iyer", ownerInitials: "MI", ownerColor: "#FF9143",
    lastEdited: "Apr 18, 2026", status: "Active", topRules: [
      "Skip if customer replied in last 7 days",
      "Promote to Quote Follow-Up on RFQ submitted",
    ],
  },
  { id: "tpl-quote", group: "sales", name: "Quote Follow-Up",
    description: "Nurture quotes from sent to closed with adaptive timing.",
    trigger: "Quote.status changed to Sent",
    steps: 5, conditions: 4, exits: 2,
    activeCount: 16, completedCount: 247, replyRate: 38, acceptRate: 22,
    owner: "Owen Brooks", ownerInitials: "OB", ownerColor: "#76ADFF",
    lastEdited: "Apr 12, 2026", status: "Active", topRules: [
      "Skip if customer replied in last 14 days",
      "Bundle multiple quotes sent the same day",
      "Exit on quote Accepted or Rejected",
    ],
  },
  { id: "tpl-renew", group: "sales", name: "Renewal Nudge",
    description: "Weekly outreach starting 60 days before contract end.",
    trigger: "Subscription.renewal_date < 60 days",
    steps: 3, conditions: 3, exits: 2,
    activeCount: 3, completedCount: 12, replyRate: 52, acceptRate: 67,
    owner: "Owen Brooks", ownerInitials: "OB", ownerColor: "#76ADFF",
    lastEdited: "Apr 04, 2026", status: "Active", topRules: [
      "Increase cadence after first reply",
      "Pause if renewal moves to negotiation",
    ],
  },
  { id: "tpl-invoice", group: "billing", name: "Invoice Reminder",
    description: "Polite-to-firm escalation for unpaid invoices.",
    trigger: "Invoice.status = 'Overdue'",
    steps: 4, conditions: 3, exits: 2,
    activeCount: 11, completedCount: 96, replyRate: 47, acceptRate: 71,
    owner: "Priya Shah", ownerInitials: "PS", ownerColor: "#5DBE8A",
    lastEdited: "Apr 09, 2026", status: "Active", topRules: [
      "Exit on payment received",
      "Pause if dispute opened",
    ],
  },
  { id: "tpl-po", group: "procurement", name: "PO Follow Up",
    description: "Chase vendor acknowledgment and confirm delivery dates.",
    trigger: "PO.status = 'Sent to Vendor'",
    steps: 3, conditions: 2, exits: 2,
    activeCount: 6, completedCount: 58, replyRate: 64, acceptRate: 88,
    owner: "Devon Yang", ownerInitials: "DY", ownerColor: "#B274FF",
    lastEdited: "Mar 30, 2026", status: "Active", topRules: [
      "Skip if vendor acknowledged",
      "Exit on delivery date confirmed",
    ],
  },
];

// Journey list — index page
const JOURNEY_LIST = [
  { id: "j-1", name: "Quote Follow-Up — Q-982510", customer: "Acme Manufacturing", template: "Quote Follow-Up", state: "midflight", status: "Active",   step: "3 of 5", nextRun: "Day 67 — Apr 25", owner: "OB", value: "$148,900" },
  { id: "j-2", name: "Quote Follow-Up — Q-981207", customer: "Northern Forge & Tool", template: "Quote Follow-Up", state: "fresh", status: "Active", step: "1 of 5", nextRun: "Day 7 — May 19", owner: "OB", value: "$84,200" },
  { id: "j-3", name: "Quote Follow-Up — Q-980441", customer: "Cascade Hydraulics", template: "Quote Follow-Up", state: "paused", status: "Paused",  step: "2 of 5", nextRun: "—", owner: "MI", value: "$32,100" },
  { id: "j-4", name: "Quote Follow-Up — Q-980022", customer: "Vector Steel Co.",  template: "Quote Follow-Up", state: "ended", status: "Completed", step: "5 of 5", nextRun: "—", owner: "OB", value: "$211,400" },
  { id: "j-5", name: "Demo No-Show Recovery",      customer: "Pinewood Castings", template: "Demo Recovery",   state: "midflight", status: "Active", step: "2 of 4", nextRun: "Day 14 — May 18", owner: "MI", value: "$22,500" },
  { id: "j-6", name: "Renewal Nudge — 60 day",     customer: "Harbor Plastics",   template: "Renewal Nudge",   state: "fresh",  status: "Active", step: "1 of 3", nextRun: "Day 30 — Jun 02", owner: "OB", value: "$58,000" },
];

// Streaming script used by the AI Builder.
// The builder visualizes this as it "thinks", then the structured journey snaps into place.
const STREAM_SCRIPT = {
  prompt: "Create a quote follow-up journey with check-ins at 7, 21, 60, and 90 days unless communication is already happening.",
  reasoning: [
    { kind: "think",   text: "Parsing intent…" },
    { kind: "think",   text: "Trigger: Quote.status → Sent" },
    { kind: "think",   text: "Scheduling 4 touchpoints at days 7, 21, 60, 90" },
    { kind: "think",   text: "Inferring conditions: customer reply, sales rep contact, quote outcome" },
    { kind: "think",   text: "Drafting skip rules to suppress redundant outreach" },
    { kind: "build",   key: "trigger",   label: "Trigger",      value: "Quote.status changed to Sent" },
    { kind: "build",   key: "step1",     label: "Day 7",        value: "Send first follow-up email" },
    { kind: "build",   key: "step2",     label: "Day 21",       value: "Send second follow-up email" },
    { kind: "build",   key: "step3",     label: "Day 60",       value: "Create sales rep task" },
    { kind: "build",   key: "step4",     label: "Day 90",       value: "Send final check-in" },
    { kind: "build",   key: "skip1",     label: "Skip rule",    value: "If customer replied in last 14 days, delay next step" },
    { kind: "build",   key: "skip2",     label: "Skip rule",    value: "If sales rep contacted customer, offset next step by 7 days" },
    { kind: "build",   key: "exit1",     label: "Exit rule",    value: "If quote is accepted or rejected, end the journey" },
    { kind: "build",   key: "exit2",     label: "Exit rule",    value: "Suppress outreach if active two-way thread exists" },
    { kind: "done",    text: "Journey ready to review." },
  ],
};

// Example prompts (tweakable)
const EXAMPLE_PROMPTS = [
  "Create a quote follow-up journey with check-ins at 7, 21, 60, and 90 days unless communication is already happening.",
  "After a demo no-show, email the contact same day, then follow up at 3 and 10 days. Skip if they reschedule.",
  "When a customer renewal is 60 days out, nudge weekly until they respond or the renewal closes.",
  "If a lead downloads a spec sheet, send a personalized email in 2 days, then a sales rep task at 7 days if no reply.",
];

Object.assign(window, {
  ICONS,
  STEP_TEMPLATE,
  JOURNEY_STATES,
  JOURNEY_LIST,
  TEMPLATES,
  TEMPLATE_GROUPS,
  STREAM_SCRIPT,
  EXAMPLE_PROMPTS,
});
