/* A3 Studio (chat) — conversation, plan & context data. */

const CHAT_PROJECT = { title: 'flight-checkin', site: 'britishairways.com', dataset: 'ada-lovelace' };

/* Selectable input datasets for the workflow run. */
const CHAT_DATASETS = [
    { id: 'ada-lovelace', label: 'ada-lovelace', hint: 'BA2490 · 1 passenger' },
    { id: 'grace-hopper', label: 'grace-hopper', hint: 'BA117 · 1 passenger' },
    { id: 'turing-family', label: 'turing-family', hint: 'LH901 · 3 passengers' },
    { id: 'qa-fixtures', label: 'qa-fixtures', hint: 'synthetic · 12 rows' },
];

const CHAT_SCOPE = {
    inputs: [
        { key: 'email', preview: '"ada@example.com"' },
        { key: 'bookingRef', preview: '"X7K2Q9"' },
        { key: 'flightNumber', preview: '"BA2490"' },
        { key: 'passengers', preview: '[{ firstName: "Ada", … }]' },
    ],
    outputs: [
        { key: 'checkedIn', preview: 'true' },
        { key: 'boardingPass', preview: '{ seat: "14A", … }' },
    ],
    context: [
        { key: 'browser.url', preview: '"…/check-in/passenger"' },
        { key: 'stage.current', preview: '"fill-passenger"' },
        { key: 'session.exitCode', preview: 'null' },
    ],
};

/* Full JSON values for the data panel's tree viewer. */
const CHAT_SCOPE_DATA = {
    inputs: {
        email: 'ada@example.com',
        bookingRef: 'X7K2Q9',
        flightNumber: 'BA2490',
        passengers: [
            { firstName: 'Ada', lastName: 'Lovelace', title: 'Ms', dateOfBirth: '1815-12-10', frequentFlyer: 'BA-88210032' },
        ],
        preferences: { seat: 'window', meal: 'vegetarian', bags: 1 },
    },
    outputs: {
        checkedIn: true,
        boardingPass: { seat: '14A', gate: 'A12', boarding: '10:55', zone: 2, barcode: 'M1LOVELACE/ADA' },
        warnings: [],
    },
    context: {
        browser: { url: 'https://britishairways.com/check-in/passenger', title: 'Passenger details', viewport: { width: 1280, height: 800 } },
        stage: { current: 'fill-passenger', index: 2, total: 5 },
        session: { id: 'sess_8f2a1c', runner: 'chrome', exitCode: null, startedAt: '2026-06-16T12:04:01Z' },
    },
};

const CHAT_PLAN = [
    { name: 'open-site', status: 'pending' },
    { name: 'login', status: 'pending' },
    { name: 'fill-passenger', status: 'pending' },
    { name: 'select-seat', status: 'pending' },
    { name: 'confirm', status: 'pending' },
];

/* Learning-orchestrator mode — learn ALL workflows at once by chaining
   them. The rail shows every workflow's stages in sequence; the agent
   walks the chain, learning each stage and carrying outputs forward. */
const ORCHESTRATOR_PLAN = [
    { workflow: 'flight-checkin', site: 'britishairways.com', stages: [
        { name: 'open-site', status: 'pending' },
        { name: 'login', status: 'pending' },
        { name: 'fill-passenger', status: 'pending' },
        { name: 'select-seat', status: 'pending' },
        { name: 'confirm', status: 'pending' },
    ] },
    { workflow: 'seat-upgrade', site: 'britishairways.com', stages: [
        { name: 'open-site', status: 'pending' },
        { name: 'login', status: 'pending' },
        { name: 'pick-cabin', status: 'pending' },
        { name: 'pay', status: 'pending' },
    ] },
    { workflow: 'refund-request', site: 'lufthansa.com', stages: [
        { name: 'open-site', status: 'pending' },
        { name: 'find-booking', status: 'pending' },
        { name: 'claim', status: 'pending' },
        { name: 'submit', status: 'pending' },
    ] },
];

const ORCHESTRATOR_THREAD = [
    { role: 'user', text: 'Learn all three workflows end to end — chain them so a checked-in booking flows into an upgrade and, if delayed, a refund.' },
    { role: 'agent', text: "I'll learn them as one chain: **flight-checkin → seat-upgrade → refund-request**, carrying each workflow's outputs into the next. Let me read the missions first." },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/flight-checkin.mission.md', result: '22 lines · 5 stages', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/seat-upgrade.mission.md', result: '14 lines · 4 stages', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/refund-request.mission.md', result: '13 lines · 4 stages', status: 'ok' },
    { role: 'agent', text: "Starting with **flight-checkin**. I'll author each stage and run it against britishairways.com." },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'flight-checkin / open-site', result: 'passed · check-in loaded', status: 'ok', ostages: { 'flight-checkin/open-site': 'executed' }, screen: 'open-site' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'flight-checkin / login', result: 'passed · authenticated', status: 'ok', ostages: { 'flight-checkin/login': 'executed' }, screen: 'login' },
    { role: 'agent', text: "The `fill-passenger` title field is a custom combobox, not a native select. Writing it to drive via keyboard.", ostages: { 'flight-checkin/fill-passenger': 'executing' } },
    { role: 'diff', file: 'flight-checkin/fill-passenger.stage.ts', added: 3, removed: 1, hunks: [
        { type: 'ctx', text: "    await this.type('#lastName', pax.lastName);" },
        { type: 'del', text: "    await this.select('#title', pax.title);" },
        { type: 'add', text: "    await this.click('#title');" },
        { type: 'add', text: "    await this.keyboard.type(pax.title);" },
        { type: 'add', text: "    await this.keyboard.press('Enter');" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'flight-checkin / fill-passenger', result: 'passed · passenger filled', status: 'ok', ostages: { 'flight-checkin/fill-passenger': 'executed' }, screen: 'fill-passenger' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'flight-checkin / select-seat → confirm', result: 'passed · seat 14A · checked in', status: 'ok', ostages: { 'flight-checkin/select-seat': 'executed', 'flight-checkin/confirm': 'executed' }, screen: 'confirm' },
    { role: 'agent', text: "**flight-checkin** is fully learned. Chaining its `boardingPass` and `bookingRef` into **seat-upgrade**." },
    { role: 'tool', icon: 'far fa-link', name: 'chain', args: 'flight-checkin.output → seat-upgrade.input', result: 'bookingRef, cabin mapped', status: 'ok' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'seat-upgrade / open-site → login', result: 'passed · Manage My Booking', status: 'ok', ostages: { 'seat-upgrade/open-site': 'executed', 'seat-upgrade/login': 'executed' }, screen: 'login' },
    { role: 'agent', text: "Learning `pick-cabin` — the availability grid lazy-loads, so I'll wait for it before selecting.", ostages: { 'seat-upgrade/pick-cabin': 'executing' } },
    { role: 'diff', file: 'seat-upgrade/pick-cabin.stage.ts', added: 2, removed: 0, hunks: [
        { type: 'ctx', text: "  async run(ctx) {" },
        { type: 'add', text: "    await this.waitFor('.cabin-grid .available');" },
        { type: 'add', text: "    await this.click(`[data-cabin=\"${ctx.inputs.cabin}\"]`);" },
        { type: 'ctx', text: "    await this.click('button.confirm-cabin');" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'seat-upgrade / pick-cabin', result: "Error: timeout waiting for .cabin-grid", status: 'error', ostages: { 'seat-upgrade/pick-cabin': 'failed' }, screen: 'select-seat' },
    { role: 'agent', text: "The grid sits inside an iframe. Scoping the wait to the frame and retrying.", ostages: { 'seat-upgrade/pick-cabin': 'executing' } },
    { role: 'diff', file: 'seat-upgrade/pick-cabin.stage.ts', added: 1, removed: 1, hunks: [
        { type: 'del', text: "    await this.waitFor('.cabin-grid .available');" },
        { type: 'add', text: "    await this.frame('#booking').waitFor('.cabin-grid .available');" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'seat-upgrade / pick-cabin → pay', result: 'passed · upgraded to Club (2A)', status: 'ok', ostages: { 'seat-upgrade/pick-cabin': 'executed', 'seat-upgrade/pay': 'executed' }, screen: 'select-seat' },
    { role: 'agent', text: "**seat-upgrade** learned. Now **refund-request** on lufthansa.com — chaining the booking reference across sites." },
    { role: 'tool', icon: 'far fa-link', name: 'chain', args: 'seat-upgrade.output → refund-request.input', result: 'bookingRef mapped', status: 'ok' },
    { role: 'diff', file: 'refund-request/find-booking.stage.ts', added: 2, removed: 0, hunks: [
        { type: 'add', text: "    await this.type('#pnr', ctx.inputs.bookingRef);" },
        { type: 'add', text: "    await this.click('button.find');" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'refund-request / find-booking → claim', result: 'passed · delayed segment selected', status: 'ok', ostages: { 'refund-request/open-site': 'executed', 'refund-request/find-booking': 'executed', 'refund-request/claim': 'executed' }, screen: 'open-site' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'refund-request / submit', result: 'passed · ref RF-4471 · £210', status: 'ok', ostages: { 'refund-request/submit': 'executed' }, screen: 'confirm' },
    { role: 'agent', text: "All three workflows are learned and chained: a check-in feeds an upgrade, and a delayed booking flows into a refund. **13/13 stages** passing end to end." },
    { role: 'cta', to: 'run', text: "All three workflows are learned and chained. You can now talk to them all at once as agent tools.", link: 'Open Chat mode →' },
];

/* The agent conversation — fixing a failing stage. */
const CHAT_THREAD = [
    { role: 'user', text: 'The fill-passenger stage keeps failing on the title dropdown. Can you take a look and fix it?' },
    { role: 'agent', text: "Let me reproduce it first — I'll read the stage and run it against the live site." },
    { role: 'tool', icon: 'far fa-file-code', name: 'read', args: 'stages/fill-passenger.stage.ts', result: '14 lines', status: 'ok' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'fill-passenger', result: "Error: select('#title') — element is not a <select>", status: 'error', stages: { 'open-site': 'executed', 'login': 'executed', 'fill-passenger': 'failed' }, screen: 'fill-passenger' },
    { role: 'agent', text: "Found it. `#title` isn't a native `<select>` — it's a custom combobox widget, so `this.select()` can't drive it. I'll open it with a click and pick the option with the keyboard instead." },
    { role: 'diff', file: 'stages/fill-passenger.stage.ts', added: 3, removed: 1, hunks: [
        { type: 'ctx', text: "    await this.type('#lastName', pax.lastName);" },
        { type: 'del', text: "    await this.select('#title', pax.title);" },
        { type: 'add', text: "    await this.click('#title');" },
        { type: 'add', text: "    await this.keyboard.type(pax.title);" },
        { type: 'add', text: "    await this.keyboard.press('Enter');" },
        { type: 'ctx', text: "    await this.click('button.continue');" },
    ], fullFile: [
        { type: 'ctx', text: "// fill-passenger.stage.ts" },
        { type: 'ctx', text: "" },
        { type: 'ctx', text: "import { Stage } from '@athree/runner';" },
        { type: 'ctx', text: "" },
        { type: 'ctx', text: "export default class FillPassenger extends Stage {" },
        { type: 'ctx', text: "    async run(ctx) {" },
        { type: 'ctx', text: "        const pax = ctx.inputs.passengers[0];" },
        { type: 'ctx', text: "        await this.type('#firstName', pax.firstName);" },
        { type: 'ctx', text: "        await this.type('#lastName', pax.lastName);" },
        { type: 'del', text: "        await this.select('#title', pax.title);" },
        { type: 'add', text: "        await this.click('#title');" },
        { type: 'add', text: "        await this.keyboard.type(pax.title);" },
        { type: 'add', text: "        await this.keyboard.press('Enter');" },
        { type: 'ctx', text: "        await this.click('button.continue');" },
        { type: 'ctx', text: "        return { matched: true };" },
        { type: 'ctx', text: "    }" },
        { type: 'ctx', text: "}" },
    ] },
    { role: 'tool', icon: 'far fa-pen', name: 'edit', args: 'stages/fill-passenger.stage.ts', result: '+3 −1', status: 'ok' },
    { role: 'agent', text: "Re-running the stage against the live page to confirm the fix." },
    { role: 'tool', icon: 'far fa-location-dot', name: 'match stage', args: 'fill-passenger', result: 'matched', status: 'ok', stages: { 'fill-passenger': 'executing' }, screen: 'fill-passenger' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'fill-passenger', result: 'passed · title=Ms, passenger filled', status: 'ok', stages: { 'fill-passenger': 'executed' }, screen: 'confirm' },
    { role: 'agent', text: "Fixed. **fill-passenger** now matches — the title is selected via keyboard and the passenger details fill cleanly. Two stages left: `select-seat` and `confirm`. Want me to continue the run?" },
];

/* The workflows in this project — in CHAT mode each is exposed to the
   agent as a single tool (you talk to all of them at once). */
const CHAT_WORKFLOWS = [
    { name: 'flight-checkin', site: 'britishairways.com', desc: 'Check a passenger into a flight' },
    { name: 'seat-upgrade', site: 'britishairways.com', desc: 'Bid for or buy a seat upgrade' },
    { name: 'refund-request', site: 'lufthansa.com', desc: 'File a delay/cancellation refund' },
];

/* Chat mode — every WORKFLOW is exposed to the agent as a tool. You
   talk to all workflows at once; the agent invokes whole workflows. */
const CHAT_THREAD_RUN = [
    { role: 'user', text: "Ada's BA2490 is delayed. Check her in with a window seat, then file a refund for the delay." },
    { role: 'agent', text: 'I have **3 workflows** available as tools. I’ll run them against their sites with the `ada-lovelace` dataset.' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'flight-checkin', args: '{ seat: "window" }', result: 'ok · checked in · seat 14A', status: 'ok', screen: 'confirm' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'refund-request', args: '{ reason: "delay >2h" }', result: 'ok · £210 refund filed · ref RF-4471', status: 'ok', screen: 'confirm' },
    { role: 'agent', text: "Done — Ada is checked in on **BA2490 (LHR → JFK)** in seat **14A**, and a **£210** delay refund is filed (ref `RF-4471`). Want me to also try a `seat-upgrade` to Club?" },
];

/* Project-config mode — the left pane shows the project's source tree
   (missions, workflows, schemas, libraries, skills) and the agent helps
   you author them. */
const CONFIG_TREE = [
    { type: 'folder', name: 'missions', children: [
        { type: 'file', name: 'flight-checkin.mission.md', path: 'flight-checkin.mission.md' },
        { type: 'file', name: 'seat-upgrade.mission.md', path: 'seat-upgrade.mission.md' },
        { type: 'file', name: 'refund-request.mission.md', path: 'refund-request.mission.md' },
    ] },
    { type: 'folder', name: 'workflows', children: [
        { type: 'file', name: 'flight-checkin.workflow.ts', path: 'flight-checkin.workflow.ts' },
        { type: 'file', name: 'seat-upgrade.workflow.ts', path: 'seat-upgrade.workflow.ts' },
        { type: 'file', name: 'refund-request.workflow.ts', path: 'refund-request.workflow.ts' },
    ] },
    { type: 'folder', name: 'schemas', children: [
        { type: 'file', name: 'passenger.schema.ts', path: 'passenger.schema.ts' },
        { type: 'file', name: 'booking.schema.ts', path: 'booking.schema.ts' },
    ] },
    { type: 'folder', name: 'libraries', children: [
        { type: 'file', name: 'airlines.lib.ts', path: 'airlines.lib.ts' },
        { type: 'file', name: 'dates.lib.ts', path: 'dates.lib.ts' },
    ] },
    { type: 'folder', name: 'skills', children: [
        { type: 'file', name: 'cookie-messages.md', path: 'cookie-messages.md' },
        { type: 'file', name: 'captcha.md', path: 'captcha.md' },
        { type: 'file', name: 'two-factor.md', path: 'two-factor.md' },
    ] },
    { type: 'file', name: 'project.config.ts', path: 'project.config.ts' },
];

const CONFIG_FILES = {
    'flight-checkin.mission.md': [
        "# Flight check-in mission",
        "",
        "Check a passenger into their flight on **britishairways.com**.",
        "Follow these steps to author the stage files under `stages/`.",
        "",
        "## Stages to build",
        "1. **open-site** — navigate to `/check-in` and wait for the form.",
        "2. **login** — enter `email` + `bookingRef`, submit, await nav.",
        "3. **fill-passenger** — type the passenger name. The title field is",
        "   a custom combobox: click it, then keyboard-select the value.",
        "4. **select-seat** — pick a seat per `preferences.seat`.",
        "5. **confirm** — submit and capture the boarding pass.",
        "",
        "## Inputs",
        "- `passenger` — see `schemas/passenger.schema.ts`",
        "- `bookingRef`, `flightNumber`",
        "",
        "## Output",
        "- `checkedIn: boolean`",
        "- `boardingPass: { seat, gate, boarding }`",
        "",
        "> Build these stages in Learn mode, one at a time, matching each",
        "> against the live site before moving on.",
    ],
    'seat-upgrade.mission.md': [
        "# Seat upgrade mission",
        "",
        "Bid for or buy a cabin upgrade on **britishairways.com**.",
        "",
        "## Stages to build",
        "1. **open-site** — open Manage My Booking.",
        "2. **login** — authenticate with `bookingRef`.",
        "3. **pick-cabin** — choose the target cabin from availability.",
        "4. **pay** — complete payment if an upgrade is offered.",
        "",
        "## Output",
        "- `seat: string`, `cabin: string`",
    ],
    'refund-request.mission.md': [
        "# Refund request mission",
        "",
        "File a delay / cancellation refund on **lufthansa.com**.",
        "",
        "## Stages to build",
        "1. **open-site** — open the refund centre.",
        "2. **find-booking** — look up the booking by reference.",
        "3. **claim** — select the delayed segment and reason.",
        "4. **submit** — submit the claim and capture the reference.",
        "",
        "## Output",
        "- `refundRef: string`, `amount: number`",
    ],
    'flight-checkin.workflow.ts': [
        "// flight-checkin.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'flight-checkin',",
        "    site: 'britishairways.com',",
        "    mission: 'flight-checkin.mission.md',",
        "    stages: ['open-site', 'login', 'fill-passenger', 'select-seat', 'confirm'],",
        "    output: { checkedIn: 'boolean', boardingPass: 'object' },",
        "});",
    ],
    'seat-upgrade.workflow.ts': [
        "// seat-upgrade.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'seat-upgrade',",
        "    site: 'britishairways.com',",
        "    mission: 'seat-upgrade.mission.md',",
        "    stages: ['open-site', 'login', 'pick-cabin', 'pay'],",
        "    output: { seat: 'string', cabin: 'string' },",
        "});",
    ],
    'refund-request.workflow.ts': [
        "// refund-request.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'refund-request',",
        "    site: 'lufthansa.com',",
        "    mission: 'refund-request.mission.md',",
        "    stages: ['open-site', 'find-booking', 'claim', 'submit'],",
        "    output: { refundRef: 'string', amount: 'number' },",
        "});",
    ],
    'passenger.schema.ts': [
        "// passenger.schema.ts",
        "import { z } from 'zod';",
        "",
        "export const passenger = z.object({",
        "    firstName: z.string().min(1),",
        "    lastName: z.string().min(1),",
        "    title: z.enum(['Mr', 'Ms', 'Mx', 'Dr']),",
        "    dateOfBirth: z.coerce.date(),",
        "    frequentFlyer: z.string().optional(),",
        "});",
        "",
        "export type Passenger = z.infer<typeof passenger>;",
    ],
    'booking.schema.ts': [
        "// booking.schema.ts",
        "import { z } from 'zod';",
        "",
        "export const booking = z.object({",
        "    bookingRef: z.string().length(6),",
        "    flightNumber: z.string().regex(/^[A-Z]{2}\\d{1,4}$/),",
        "    origin: z.string().length(3),",
        "    destination: z.string().length(3),",
        "});",
        "",
        "export type Booking = z.infer<typeof booking>;",
    ],
    'airlines.lib.ts': [
        "// airlines.lib.ts",
        "export const airlines = {",
        "    BA: { name: 'British Airways', site: 'britishairways.com' },",
        "    LH: { name: 'Lufthansa', site: 'lufthansa.com' },",
        "};",
    ],
    'dates.lib.ts': [
        "// dates.lib.ts",
        "export const fmtDate = (d) => new Date(d).toISOString().slice(0, 10);",
    ],
    'cookie-messages.md': [
        "# Skill: dismiss cookie messages",
        "",
        "A reusable skill any stage can apply before interacting with a page.",
        "Most sites show a consent banner that blocks clicks until dismissed.",
        "",
        "## When to use",
        "- Right after `open-site`, before the first interaction.",
        "- Whenever a click is intercepted by an overlay.",
        "",
        "## Steps",
        "1. Wait up to 3s for a consent dialog (`[id*=cookie]`, `[class*=consent]`).",
        "2. Prefer the **reject-all** control; fall back to **accept-all**.",
        "3. Confirm the overlay is gone before continuing.",
        "",
        "## Selectors to try",
        "- `button:has-text(\"Reject all\")`, `button:has-text(\"Accept all\")`",
        "- `#onetrust-reject-all-handler`, `.cookie-banner button`",
        "",
        "> If no banner appears within the timeout, continue silently.",
    ],
    'captcha.md': [
        "# Skill: handle captcha",
        "",
        "Detect and route around CAPTCHA challenges.",
        "",
        "## Steps",
        "1. Detect a challenge (`iframe[src*=recaptcha]`, `[class*=captcha]`).",
        "2. Pause the run and request a solve via the configured provider.",
        "3. Resume once the token is injected; retry the blocked action.",
        "",
        "> If unsolvable, fail the stage with `reason: 'captcha'`.",
    ],
    'two-factor.md': [
        "# Skill: two-factor / OTP",
        "",
        "Complete a one-time-password challenge during login.",
        "",
        "## Steps",
        "1. Detect the OTP field (`input[autocomplete=one-time-code]`).",
        "2. Fetch the code from the dataset or the connected inbox/SMS hook.",
        "3. Type the code and submit; wait for navigation.",
    ],
    'project.config.ts': [
        "// project.config.ts",
        "export default {",
        "    project: 'flight-management',",
        "    runner: 'chrome',",
        "    dataset: 'ada-lovelace',",
        "};",
    ],
};

const CHAT_THREAD_CONFIG = [
    { role: 'agent', text: "Let's set up your **flight-management** project. I'll work bottom-up: first scaffold the reusable **libraries** and **skills**, then reason about the data **schemas**, then write a **mission** per workflow, and finally the **workflow** definitions that tie it together. What airlines and tasks should it cover?" },
    { role: 'user', text: 'British Airways and Lufthansa — check-in, seat upgrades, and delay refunds.' },
    { role: 'agent', text: "Got it — three workflows across two sites. Starting with the project config and the shared building blocks, since missions and workflows will depend on them." },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'project.config.ts', cfile: 'project.config.ts', result: '+5 lines', status: 'ok' },
    { role: 'agent', text: "Scaffolding the **libraries** (airline metadata, date helpers) and the **skills** every stage can reuse — dismissing cookie banners, solving captchas, and OTP login. These are generic, so I can drop them in immediately." },
    { role: 'tool', kind: 'config', icon: 'far fa-folder-plus', name: 'scaffold', args: 'libraries/ + skills/', cfiles: ['libraries/airlines.lib.ts', 'libraries/dates.lib.ts', 'skills/cookie-messages.md', 'skills/captcha.md', 'skills/two-factor.md'], result: '5 files · 2 libraries, 3 skills', status: 'ok' },
    { role: 'agent', text: "Now the **schemas**. Before writing them I need to think about the data each workflow touches: check-in and upgrade both operate on a **passenger** (name, title, date of birth, frequent-flyer number) and a **booking** (reference, flight number, route). Refund reuses the same booking. So two shared schemas cover all three workflows — I'll model them in Zod so inputs are validated everywhere." },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'schemas/passenger.schema.ts', cfile: 'schemas/passenger.schema.ts', result: '+11 lines', status: 'ok' },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'schemas/booking.schema.ts', cfile: 'schemas/booking.schema.ts', result: '+11 lines', status: 'ok' },
    { role: 'agent', text: "With the schemas in place I can write the **missions** — a Markdown brief per workflow that the agent follows to build the stage files. Each lists the stages, the inputs (referencing the schemas above), and the expected output." },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'missions/flight-checkin.mission.md', cfile: 'missions/flight-checkin.mission.md', result: '+22 lines', status: 'ok' },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'missions/seat-upgrade.mission.md', cfile: 'missions/seat-upgrade.mission.md', result: '+12 lines', status: 'ok' },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'missions/refund-request.mission.md', cfile: 'missions/refund-request.mission.md', result: '+12 lines', status: 'ok' },
    { role: 'agent', text: "Finally the **workflow** definitions — each binds its mission to a site, declares the stage list, and types its output so the orchestrator can chain workflows together later." },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'workflows/flight-checkin.workflow.ts', cfile: 'workflows/flight-checkin.workflow.ts', result: '+10 lines', status: 'ok' },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'workflows/seat-upgrade.workflow.ts', cfile: 'workflows/seat-upgrade.workflow.ts', result: '+10 lines', status: 'ok' },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'workflows/refund-request.workflow.ts', cfile: 'workflows/refund-request.workflow.ts', result: '+10 lines', status: 'ok' },
    { role: 'agent', text: "Project scaffolded — **16 files**: 2 libraries, 3 skills, 2 Zod schemas, 3 missions and 3 workflows. Each mission lists the stages to build; open one on the left to review it, then learn the workflows in the orchestrator." },
    { role: 'cta', to: 'orchestrate', text: "Your project is configured. Next, learn all the workflows at once by chaining them together.", link: 'Open the Learning orchestrator →' },
];

/* Per-stage source files — the post-fix (clean, diff-free) contents,
   shown in the right-pane editor when a stage is opened. */
const STAGE_FILES = {
    'open-site': [
        "// open-site.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class OpenSite extends Stage {",
        "    async run(ctx) {",
        "        await this.goto('https://britishairways.com/check-in');",
        "        await this.waitFor('main.check-in');",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'login': [
        "// login.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class Login extends Stage {",
        "    async run(ctx) {",
        "        await this.type('#email', ctx.inputs.email);",
        "        await this.type('#bookingRef', ctx.inputs.bookingRef);",
        "        await this.click('button.sign-in');",
        "        await this.waitForNavigation();",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'fill-passenger': [
        "// fill-passenger.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class FillPassenger extends Stage {",
        "    async run(ctx) {",
        "        const pax = ctx.inputs.passengers[0];",
        "        await this.type('#firstName', pax.firstName);",
        "        await this.type('#lastName', pax.lastName);",
        "        await this.click('#title');",
        "        await this.keyboard.type(pax.title);",
        "        await this.keyboard.press('Enter');",
        "        await this.click('button.continue');",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'select-seat': [
        "// select-seat.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class SelectSeat extends Stage {",
        "    async run(ctx) {",
        "        await this.click('.seat-map .available[data-window]');",
        "        await this.click('button.confirm-seat');",
        "        return { matched: true, seat: this.read('.selected-seat') };",
        "    }",
        "}",
    ],
    'confirm': [
        "// confirm.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class Confirm extends Stage {",
        "    async run(ctx) {",
        "        await this.click('button.complete-checkin');",
        "        await this.waitFor('.boarding-pass');",
        "        return { matched: true, checkedIn: true };",
        "    }",
        "}",
    ],
};

/* Empty-state suggested prompts, per mode. Each has a headline title and
   the prompt text that gets "typed" when clicked. */
const CHAT_SUGGESTIONS = {
    learn: {
        title: 'Build a workflow with A3',
        blurb: 'Describe a task on a site and A3 learns each stage against the live page.',
        items: [
            { icon: 'far fa-plane-departure', label: 'Onboard ba.com check-in', prompt: 'Onboard britishairways.com — learn a flight check-in workflow stage by stage.' },
            { icon: 'far fa-wrench', label: 'Fix the fill-passenger stage', prompt: 'The fill-passenger stage keeps failing on the title dropdown. Can you take a look and fix it?' },
            { icon: 'far fa-vial', label: 'Add a select-seat stage', prompt: 'Add a select-seat stage that picks a window seat after fill-passenger.' },
        ],
    },
    orchestrate: {
        title: 'Learn every workflow at once',
        blurb: 'A3 learns all workflows and chains each one’s output into the next.',
        items: [
            { icon: 'far fa-link', label: 'Start learning workflows', prompt: 'Learn all three workflows end to end — chain them so a checked-in booking flows into an upgrade and, if delayed, a refund.' },
        ],
    },
    run: {
        title: 'Chat with your workflows',
        blurb: 'Every workflow is exposed to the agent as a tool — just say what you need.',
        items: [
            { icon: 'far fa-plane', label: 'Check Ada in & refund the delay', prompt: "Ada's BA2490 is delayed. Check her in with a window seat, then file a refund for the delay." },
            { icon: 'far fa-chair', label: 'Upgrade Ada to Club', prompt: 'Upgrade Ada to Club World on her BA2490 flight.' },
        ],
    },
    config: {
        title: 'Set up your project',
        blurb: 'A3 writes the missions, workflows and Zod schemas for your task.',
        items: [
            { icon: 'far fa-folder-gear', label: 'Scaffold flight-management', prompt: 'Set up a flight-management project for British Airways and Lufthansa — check-in, seat upgrades, and delay refunds.' },
            { icon: 'far fa-file-pen', label: 'Add a passenger schema', prompt: 'Add a Zod passenger schema with title, names, date of birth and frequent flyer number.' },
        ],
    },
};

window.A3ChatData = { CHAT_PROJECT, CHAT_DATASETS, CHAT_SUGGESTIONS, CHAT_SCOPE, CHAT_SCOPE_DATA, CHAT_WORKFLOWS, CHAT_PLAN, ORCHESTRATOR_PLAN, ORCHESTRATOR_THREAD, CHAT_THREAD, CHAT_THREAD_RUN, CHAT_THREAD_CONFIG, CONFIG_TREE, CONFIG_FILES, STAGE_FILES };
