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

const CHAT_PROJECT = { title: 'apply-role', site: 'spacex.com', dataset: 'okafor-eng' };

/* Selectable input datasets for the workflow run. */
const CHAT_DATASETS = [
    { id: 'okafor-eng', label: 'okafor-eng', hint: 'Software Engineer · Hawthorne' },
    { id: 'chen-propulsion', label: 'chen-propulsion', hint: 'Propulsion Engineer · McGregor' },
    { id: 'grad-2026', label: 'grad-2026', hint: 'New-grad · 3 roles' },
    { id: 'qa-fixtures', label: 'qa-fixtures', hint: 'synthetic · 12 rows' },
];

const CHAT_SCOPE = {
    inputs: [
        { key: 'applicant', preview: '{ firstName: "Daniel", … }' },
        { key: 'role', preview: '"Software Engineer, Starlink"' },
        { key: 'reqId', preview: '"SX-3192"' },
        { key: 'resumeFile', preview: '"daniel-okafor-resume.pdf"' },
    ],
    outputs: [
        { key: 'submitted', preview: 'true' },
        { key: 'application', preview: '{ ref: "SX-3192", … }' },
    ],
    context: [
        { key: 'browser.url', preview: '"…/careers/apply"' },
        { key: 'stage.current', preview: '"application"' },
        { key: 'session.exitCode', preview: 'null' },
    ],
};

/* Full JSON values for the data panel's tree viewer. */
const CHAT_SCOPE_DATA = {
    inputs: {
        applicant: { firstName: 'Daniel', lastName: 'Okafor', title: 'Mr', email: 'daniel@example.com', phone: '+1 310 555 0184', yearsExperience: 6 },
        role: { reqId: 'SX-3192', title: 'Software Engineer, Starlink', location: 'Hawthorne, CA', team: 'Starlink', employmentType: 'full-time' },
        reqId: 'SX-3192',
        resumeFile: 'daniel-okafor-resume.pdf',
        links: { linkedIn: 'https://linkedin.com/in/danielokafor', github: 'https://github.com/danielokafor' },
    },
    outputs: {
        submitted: true,
        application: { ref: 'SX-3192', role: 'Software Engineer, Starlink', location: 'Hawthorne, CA', status: 'received', resume: 'daniel-okafor-resume.pdf' },
        warnings: [],
    },
    context: {
        browser: { url: 'https://spacex.com/careers/apply', title: 'Apply', viewport: { width: 1280, height: 800 } },
        stage: { current: 'application', index: 3, total: 7 },
        session: { id: 'sess_b41d7e', runner: 'chrome', exitCode: null, startedAt: '2026-06-18T09:21:04Z' },
    },
};

const CHAT_PLAN = [
    { name: 'open-site', status: 'pending' },
    { name: 'find-role', status: 'pending' },
    { name: 'fill-form', status: 'pending' },
    { name: 'upload-resume', status: 'pending' },
    { name: 'submit', status: 'pending' },
];

/* Learning 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. The job-application
   funnel is modelled as seven small workflows, one per funnel step. */
const ORCHESTRATOR_PLAN = [
    { workflow: 'search-roles', site: 'spacex.com', stages: [
        { name: 'open-site', status: 'pending' },
        { name: 'search', status: 'pending' },
    ] },
    { workflow: 'role-details', site: 'spacex.com', stages: [
        { name: 'open-role', status: 'pending' },
    ] },
    { workflow: 'personal-details', site: 'spacex.com', stages: [
        { name: 'enter-personal', status: 'pending' },
    ] },
    { workflow: 'work-experience', site: 'spacex.com', stages: [
        { name: 'add-experience', status: 'pending' },
        { name: 'add-education', status: 'pending' },
    ] },
    { workflow: 'upload-resume', site: 'spacex.com', stages: [
        { name: 'attach-file', status: 'pending' },
    ] },
    { workflow: 'review', site: 'spacex.com', stages: [
        { name: 'check-answers', status: 'pending' },
    ] },
    { workflow: 'submit-application', site: 'spacex.com', stages: [
        { name: 'submit', status: 'pending' },
    ] },
];

const ORCHESTRATOR_THREAD = [
    { role: 'user', text: 'learn spacex.com' },
    { role: 'agent', text: "I'll learn them as one chain: **search-roles → role-details → personal-details → work-experience → upload-resume → review → submit-application**, 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/search-roles.mission.md', result: '13 lines · 2 stages', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/role-details.mission.md', result: '10 lines · 1 stage', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/personal-details.mission.md', result: '12 lines · 1 stage', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/work-experience.mission.md', result: '12 lines · 2 stages', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/upload-resume.mission.md', result: '10 lines · 1 stage', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/review.mission.md', result: '9 lines · 1 stage', status: 'ok' },
    { role: 'tool', icon: 'far fa-file-lines', name: 'read', args: 'missions/submit-application.mission.md', result: '9 lines · 1 stage', status: 'ok' },
    { role: 'agent', text: "Starting with **search-roles**. I'll author each stage and run it against spacex.com." },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'search-roles / open-site', result: 'passed · spacex.com/careers loaded', status: 'ok', ostages: { 'search-roles/open-site': 'executed' }, screen: 'listing' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'search-roles / search', result: 'passed · 24 engineering roles', status: 'ok', ostages: { 'search-roles/search': 'executed' }, screen: 'listing' },
    { role: 'agent', text: "**search-roles** is fully learned. Chaining the matched role into **role-details**." },
    { role: 'tool', icon: 'far fa-link', name: 'chain', args: 'search-roles.output → role-details.input', result: 'role, keywords mapped', status: 'ok' },
    { role: 'agent', text: "The role list is virtualised, so I'll match the row by its title text before opening it.", ostages: { 'role-details/open-role': 'executing' } },
    { role: 'diff', file: 'role-details/open-role.stage.ts', added: 2, removed: 1, hunks: [
        { type: 'ctx', text: "    await this.type('#keywords', ctx.inputs.role.title);" },
        { type: 'del', text: "    await this.click('.role-row:first-child a');" },
        { type: 'add', text: "    await this.click(`a:has-text(\"${ctx.inputs.role.title}\")`);" },
        { type: 'add', text: "    await this.waitFor('main.role-detail');" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'role-details / open-role', result: 'passed · reqId SX-3192 · application opened', status: 'ok', ostages: { 'role-details/open-role': 'executed' }, screen: 'role-detail' },
    { role: 'agent', text: "**role-details** learned. Chaining the role's `reqId` into **personal-details**." },
    { role: 'tool', icon: 'far fa-link', name: 'chain', args: 'role-details.output → personal-details.input', result: 'reqId, role mapped', status: 'ok' },
    { role: 'agent', text: "Learning `enter-personal` — the title field is a custom combobox, so I'll drive it by keyboard.", ostages: { 'personal-details/enter-personal': 'executing' }, screen: 'application' },
    { role: 'diff', file: 'personal-details/enter-personal.stage.ts', added: 3, removed: 1, hunks: [
        { type: 'ctx', text: "    await this.type('#lastName', a.lastName);" },
        { type: 'del', text: "    await this.select('#title', a.title);" },
        { type: 'add', text: "    await this.click('#title');" },
        { type: 'add', text: "    await this.keyboard.type(a.title);" },
        { type: 'add', text: "    await this.keyboard.press('Enter');" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'personal-details / enter-personal', result: 'passed · applicant filled', status: 'ok', ostages: { 'personal-details/enter-personal': 'executed' }, screen: 'application' },
    { role: 'agent', text: "**personal-details** learned. Now **work-experience** — entering the experience and education on the same form." },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'work-experience / add-experience → add-education', result: 'passed · 6 yrs · education added', status: 'ok', ostages: { 'work-experience/add-experience': 'executed', 'work-experience/add-education': 'executed' }, screen: 'application' },
    { role: 'agent', text: "**work-experience** learned. Now **upload-resume** — chaining the résumé file in.", ostages: { 'work-experience/add-education': 'executed' } },
    { role: 'tool', icon: 'far fa-link', name: 'chain', args: 'work-experience.output → upload-resume.input', result: 'resumeFile mapped', status: 'ok' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'upload-resume / attach-file', result: 'Error: file input is hidden — click intercepted', status: 'error', ostages: { 'upload-resume/attach-file': 'failed' }, screen: 'resume-upload' },
    { role: 'agent', text: "The drop zone hides the real `<input type=file>`, so the click never reaches it. Setting the file on the input directly instead.", ostages: { 'upload-resume/attach-file': 'executing' } },
    { role: 'diff', file: 'upload-resume/attach-file.stage.ts', added: 1, removed: 1, hunks: [
        { type: 'del', text: "    await this.click('.dropzone');" },
        { type: 'add', text: "    await this.setInputFiles('input[type=file]', ctx.inputs.resumeFile);" },
    ] },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'upload-resume / attach-file', result: 'passed · daniel-okafor-resume.pdf uploaded', status: 'ok', ostages: { 'upload-resume/attach-file': 'executed' }, screen: 'resume-upload' },
    { role: 'agent', text: "**upload-resume** learned. Now **review** — checking every answer before submitting." },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'review / check-answers', result: 'passed · all answers valid · ready to submit', status: 'ok', ostages: { 'review/check-answers': 'executed' }, screen: 'review' },
    { role: 'agent', text: "**review** learned. Finally **submit-application** — submitting and capturing the reference." },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'submit-application / submit', result: 'passed · ref SX-3192 · received', status: 'ok', ostages: { 'submit-application/submit': 'executed' }, screen: 'confirmation' },
    { role: 'agent', text: "All seven workflows are learned and chained: the search feeds the role page, the application details and résumé build up step by step, and the review feeds the final submit. **9/9 stages** passing end to end." },
    { role: 'cta', to: 'run', text: "All seven workflows are learned and chained. You can now talk to them all at once as MCP tools.", link: 'Open Chat to MCP →' },
];

/* The agent conversation — fixing a failing stage (learn-mode shape, kept
   for parity; reachable via the orchestrator's per-stage editing). */
const CHAT_THREAD = [
    { role: 'user', text: 'The fill-form stage keeps failing on the application 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-form.stage.ts', result: '13 lines', status: 'ok' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'fill-form', result: "Error: select('#title') — element is not a <select>", status: 'error', stages: { 'open-site': 'executed', 'find-role': 'executed', 'fill-form': 'failed' }, screen: 'application' },
    { role: 'agent', text: "Found it. `#title` isn't a native `<select>` — it's a custom combobox, 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-form.stage.ts', added: 3, removed: 1, hunks: [
        { type: 'ctx', text: "    await this.type('#lastName', a.lastName);" },
        { type: 'del', text: "    await this.select('#title', a.title);" },
        { type: 'add', text: "    await this.click('#title');" },
        { type: 'add', text: "    await this.keyboard.type(a.title);" },
        { type: 'add', text: "    await this.keyboard.press('Enter');" },
        { type: 'ctx', text: "    await this.click('button.continue');" },
    ] },
    { role: 'tool', icon: 'far fa-pen', name: 'edit', args: 'stages/fill-form.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-form', result: 'matched', status: 'ok', stages: { 'fill-form': 'executing' }, screen: 'application' },
    { role: 'tool', icon: 'far fa-play', name: 'run stage', args: 'fill-form', result: 'passed · title=Mr, applicant filled', status: 'ok', stages: { 'fill-form': 'executed' }, screen: 'review' },
    { role: 'agent', text: "Fixed. **fill-form** now matches — the title is selected via keyboard and the applicant details fill cleanly. Two stages left: `upload-resume` and `submit`. Want me to continue the run?" },
];

/* The workflows in this project — in Chat-to-MCP mode each is exposed to
   the agent as a single MCP tool (you talk to all of them at once). The
   seven workflows mirror the natural job-application funnel. */
const CHAT_WORKFLOWS = [
    { name: 'search-roles', site: 'spacex.com', desc: 'Open the careers site and search for roles' },
    { name: 'role-details', site: 'spacex.com', desc: 'Open a role and start the application' },
    { name: 'personal-details', site: 'spacex.com', desc: 'Fill the applicant’s personal details' },
    { name: 'work-experience', site: 'spacex.com', desc: 'Add work experience & education' },
    { name: 'upload-resume', site: 'spacex.com', desc: 'Attach the résumé file' },
    { name: 'review', site: 'spacex.com', desc: 'Review every answer before submitting' },
    { name: 'submit-application', site: 'spacex.com', desc: 'Submit the application & capture the reference' },
];

/* Chat-to-MCP mode — every WORKFLOW is exposed to the agent as a tool.
   You talk to all of them at once; the agent invokes whole workflows. */
const CHAT_THREAD_RUN = [
    { role: 'user', text: 'Apply Daniel to the Starlink Software Engineer role with his résumé.' },
    { role: 'agent', text: 'I have **7 workflows** available as MCP tools. I’ll run them against spacex.com with the `okafor-eng` dataset.' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'search-roles', args: '{ keywords: "Software Engineer, Starlink" }', result: 'ok · 24 roles · matched SX-3192', status: 'ok', screen: 'listing' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'role-details', args: '{ reqId: "SX-3192" }', result: 'ok · role opened · application started', status: 'ok', screen: 'role-detail' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'personal-details', args: '{ applicant: "okafor" }', result: 'ok · personal details filled', status: 'ok', screen: 'application' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'upload-resume', args: '{ resume: "daniel-okafor-resume.pdf" }', result: 'ok · résumé attached', status: 'ok', screen: 'resume-upload' },
    { role: 'tool', kind: 'workflow', icon: 'far fa-diagram-project', name: 'submit-application', args: '{ reqId: "SX-3192" }', result: 'ok · submitted · ref SX-3192', status: 'ok', screen: 'confirmation' },
    { role: 'agent', text: "Done — Daniel Okafor is applied to **Software Engineer, Starlink** (Hawthorne, CA) with his résumé attached (ref `SX-3192`, status received). Want me to also draft a cover note?" },
];

/* 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: 'search-roles.mission.md', path: 'search-roles.mission.md' },
        { type: 'file', name: 'role-details.mission.md', path: 'role-details.mission.md' },
        { type: 'file', name: 'personal-details.mission.md', path: 'personal-details.mission.md' },
        { type: 'file', name: 'work-experience.mission.md', path: 'work-experience.mission.md' },
        { type: 'file', name: 'upload-resume.mission.md', path: 'upload-resume.mission.md' },
        { type: 'file', name: 'review.mission.md', path: 'review.mission.md' },
        { type: 'file', name: 'submit-application.mission.md', path: 'submit-application.mission.md' },
    ] },
    { type: 'folder', name: 'workflows', children: [
        { type: 'file', name: 'search-roles.workflow.ts', path: 'search-roles.workflow.ts' },
        { type: 'file', name: 'role-details.workflow.ts', path: 'role-details.workflow.ts' },
        { type: 'file', name: 'personal-details.workflow.ts', path: 'personal-details.workflow.ts' },
        { type: 'file', name: 'work-experience.workflow.ts', path: 'work-experience.workflow.ts' },
        { type: 'file', name: 'upload-resume.workflow.ts', path: 'upload-resume.workflow.ts' },
        { type: 'file', name: 'review.workflow.ts', path: 'review.workflow.ts' },
        { type: 'file', name: 'submit-application.workflow.ts', path: 'submit-application.workflow.ts' },
    ] },
    { type: 'folder', name: 'schemas', children: [
        { type: 'file', name: 'applicant.schema.ts', path: 'applicant.schema.ts' },
        { type: 'file', name: 'role.schema.ts', path: 'role.schema.ts' },
    ] },
    { type: 'folder', name: 'libraries', children: [
        { type: 'file', name: 'careers.lib.ts', path: 'careers.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 = {
    'search-roles.mission.md': [
        "# Search roles mission",
        "",
        "Open the careers site and search for a role on **spacex.com**.",
        "Follow these steps to author the stage files under `stages/`.",
        "",
        "## Stages to build",
        "1. **open-site** — navigate to the careers page and wait for the list.",
        "2. **search** — enter the `keywords` and apply the team filter.",
        "",
        "## Inputs",
        "- `keywords`, `role` — see `schemas/role.schema.ts`",
        "",
        "## Output",
        "- `roles: { reqId, title, team }[]`",
    ],
    'role-details.mission.md': [
        "# Role details mission",
        "",
        "Open a role and start its application on **spacex.com**.",
        "",
        "## Stages to build",
        "1. **open-role** — match the role row by its title text, open it,",
        "   and click Apply to open the application form.",
        "",
        "## Inputs",
        "- `role` — see `schemas/role.schema.ts`",
        "",
        "## Output",
        "- `role: { reqId, title, location, team }`",
    ],
    'personal-details.mission.md': [
        "# Personal details mission",
        "",
        "Fill the applicant's personal details on **spacex.com**.",
        "",
        "## Stages to build",
        "1. **enter-personal** — fill the applicant. The title field is a",
        "   custom combobox: click it, then keyboard-select the value.",
        "",
        "## Inputs",
        "- `applicant` — see `schemas/applicant.schema.ts`",
        "- `reqId`, `role`",
        "",
        "## Output",
        "- `personal: { filled: boolean }`",
    ],
    'work-experience.mission.md': [
        "# Work experience mission",
        "",
        "Add work experience and education on **spacex.com**.",
        "",
        "## Stages to build",
        "1. **add-experience** — enter years of experience and links.",
        "2. **add-education** — add the highest qualification.",
        "",
        "## Inputs",
        "- `applicant` — see `schemas/applicant.schema.ts`",
        "",
        "## Output",
        "- `experience: { years: number }`",
    ],
    'upload-resume.mission.md': [
        "# Upload résumé mission",
        "",
        "Attach the résumé file on **spacex.com**.",
        "",
        "## Stages to build",
        "1. **attach-file** — set the résumé file on the hidden file input.",
        "",
        "## Inputs",
        "- `resumeFile`",
        "",
        "## Output",
        "- `uploaded: boolean`",
    ],
    'review.mission.md': [
        "# Review mission",
        "",
        "Review every answer before submitting on **spacex.com**.",
        "",
        "## Stages to build",
        "1. **check-answers** — verify each section is complete and valid.",
        "",
        "## Output",
        "- `ready: boolean`",
    ],
    'submit-application.mission.md': [
        "# Submit application mission",
        "",
        "Submit the application and capture the reference on **spacex.com**.",
        "",
        "## Stages to build",
        "1. **submit** — submit the application and wait for confirmation.",
        "",
        "## Output",
        "- `submitted: boolean`, `ref: string`",
    ],
    'search-roles.workflow.ts': [
        "// search-roles.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'search-roles',",
        "    site: 'spacex.com',",
        "    mission: 'search-roles.mission.md',",
        "    stages: ['open-site', 'search'],",
        "    output: { roles: 'array' },",
        "});",
    ],
    'role-details.workflow.ts': [
        "// role-details.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'role-details',",
        "    site: 'spacex.com',",
        "    mission: 'role-details.mission.md',",
        "    stages: ['open-role'],",
        "    output: { role: 'object' },",
        "});",
    ],
    'personal-details.workflow.ts': [
        "// personal-details.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'personal-details',",
        "    site: 'spacex.com',",
        "    mission: 'personal-details.mission.md',",
        "    stages: ['enter-personal'],",
        "    output: { personal: 'object' },",
        "});",
    ],
    'work-experience.workflow.ts': [
        "// work-experience.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'work-experience',",
        "    site: 'spacex.com',",
        "    mission: 'work-experience.mission.md',",
        "    stages: ['add-experience', 'add-education'],",
        "    output: { experience: 'object' },",
        "});",
    ],
    'upload-resume.workflow.ts': [
        "// upload-resume.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'upload-resume',",
        "    site: 'spacex.com',",
        "    mission: 'upload-resume.mission.md',",
        "    stages: ['attach-file'],",
        "    output: { uploaded: 'boolean' },",
        "});",
    ],
    'review.workflow.ts': [
        "// review.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'review',",
        "    site: 'spacex.com',",
        "    mission: 'review.mission.md',",
        "    stages: ['check-answers'],",
        "    output: { ready: 'boolean' },",
        "});",
    ],
    'submit-application.workflow.ts': [
        "// submit-application.workflow.ts",
        "import { Workflow } from '@athree/core';",
        "",
        "export default new Workflow({",
        "    name: 'submit-application',",
        "    site: 'spacex.com',",
        "    mission: 'submit-application.mission.md',",
        "    stages: ['submit'],",
        "    output: { submitted: 'boolean', ref: 'string' },",
        "});",
    ],
    'applicant.schema.ts': [
        "// applicant.schema.ts",
        "import { z } from 'zod';",
        "",
        "export const applicant = z.object({",
        "    firstName: z.string().min(1),",
        "    lastName: z.string().min(1),",
        "    title: z.enum(['Mr', 'Ms', 'Mx', 'Dr']),",
        "    email: z.string().email(),",
        "    phone: z.string().min(7),",
        "    yearsExperience: z.number().int().min(0),",
        "});",
        "",
        "export type Applicant = z.infer<typeof applicant>;",
    ],
    'role.schema.ts': [
        "// role.schema.ts",
        "import { z } from 'zod';",
        "",
        "export const role = z.object({",
        "    reqId: z.string().min(2),",
        "    title: z.string().min(2),",
        "    location: z.string().min(2),",
        "    team: z.string().min(2),",
        "    employmentType: z.enum(['full-time', 'intern', 'contract']),",
        "});",
        "",
        "export type Role = z.infer<typeof role>;",
    ],
    'careers.lib.ts': [
        "// careers.lib.ts",
        "export const company = {",
        "    name: 'SpaceX',",
        "    site: 'spacex.com',",
        "    careers: 'spacex.com/careers',",
        "    sites: ['Hawthorne, CA', 'McGregor, TX', 'Starbase, TX'],",
        "};",
        "",
        "export const reqUrl = (reqId) =>",
        "    `https://spacex.com/careers/${reqId}`;",
    ],
    '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 or payment.",
        "",
        "## 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: 'job-application',",
        "    runner: 'chrome',",
        "    dataset: 'okafor-eng',",
        "};",
    ],
};

const CHAT_THREAD_CONFIG = [
    { role: 'agent', text: "Let's set up your **job-application** 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 site and tasks should it cover?" },
    { role: 'user', text: 'SpaceX.com — model the whole application funnel: search roles, open a role, fill personal details, add work experience, upload a résumé, review, and submit.' },
    { role: 'agent', text: "Got it — seven workflows, one per funnel step, all on one site. 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** (careers-site metadata, date helpers) and the **skills** every stage can reuse — dismissing cookie banners, solving captchas, and OTP at 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/careers.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**. The funnel touches an **applicant** (title, names, email, phone, experience) and a **role** (reqId, title, location, team). Most steps reuse the same applicant. So two shared schemas cover all seven 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/applicant.schema.ts', cfile: 'schemas/applicant.schema.ts', result: '+12 lines', status: 'ok' },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'write', args: 'schemas/role.schema.ts', cfile: 'schemas/role.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. One mission per funnel step." },
    { role: 'tool', kind: 'config', icon: 'far fa-file-pen', name: 'scaffold', args: 'missions/', cfiles: ['missions/search-roles.mission.md', 'missions/role-details.mission.md', 'missions/personal-details.mission.md', 'missions/work-experience.mission.md', 'missions/upload-resume.mission.md', 'missions/review.mission.md', 'missions/submit-application.mission.md'], result: '7 files · one mission per funnel step', 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: 'scaffold', args: 'workflows/', cfiles: ['workflows/search-roles.workflow.ts', 'workflows/role-details.workflow.ts', 'workflows/personal-details.workflow.ts', 'workflows/work-experience.workflow.ts', 'workflows/upload-resume.workflow.ts', 'workflows/review.workflow.ts', 'workflows/submit-application.workflow.ts'], result: '7 files · one workflow per funnel step', status: 'ok' },
    { role: 'agent', text: "Project scaffolded — **24 files**: 2 libraries, 3 skills, 2 Zod schemas, 7 missions and 7 workflows. Each mission lists the stages to build; open one on the left to review it, then learn the workflows in Learning mode." },
    { role: 'cta', to: 'orchestrate', text: "Your project is configured. Next, learn all the workflows at once by chaining them together.", link: 'Open Learning →' },
];

/* 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://spacex.com/careers');",
        "        await this.waitFor('ul.role-list');",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'search': [
        "// search.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class Search extends Stage {",
        "    async run(ctx) {",
        "        await this.type('#keywords', ctx.inputs.role.title);",
        "        await this.click('button.search-roles');",
        "        await this.waitFor('ul.role-list li');",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'enter-personal': [
        "// enter-personal.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class EnterPersonal extends Stage {",
        "    async run(ctx) {",
        "        const a = ctx.inputs.applicant;",
        "        await this.type('#firstName', a.firstName);",
        "        await this.type('#lastName', a.lastName);",
        "        await this.click('#title');",
        "        await this.keyboard.type(a.title);",
        "        await this.keyboard.press('Enter');",
        "        await this.click('button.continue');",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'attach-file': [
        "// attach-file.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class AttachFile extends Stage {",
        "    async run(ctx) {",
        "        await this.setInputFiles('input[type=file]', ctx.inputs.resumeFile);",
        "        await this.waitFor('.upload-complete');",
        "        return { matched: true };",
        "    }",
        "}",
    ],
    'submit': [
        "// submit.stage.ts",
        "",
        "import { Stage } from '@athree/runner';",
        "",
        "export default class Submit extends Stage {",
        "    async run(ctx) {",
        "        await this.click('button.submit-application');",
        "        await this.waitFor('.application-received');",
        "        return { matched: true, submitted: true };",
        "    }",
        "}",
    ],
};

/* Empty-state suggested prompts, per mode. Each has a headline title and
   the prompt text that gets "typed" when clicked. */
const CHAT_SUGGESTIONS = {
    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-graduation-cap', label: 'Learn spacex.com', prompt: 'learn spacex.com' },
        ],
    },
    run: {
        title: 'Chat with your workflows',
        blurb: 'Every workflow is exposed to the agent as an MCP tool — just say what you need.',
        items: [
            { icon: 'far fa-user-tie', label: 'Apply Daniel to Starlink role', prompt: 'Apply Daniel to the Starlink Software Engineer role with his résumé.' },
            { icon: 'far fa-file-arrow-up', label: 'Upload a résumé to an application', prompt: 'Upload daniel-okafor-resume.pdf to application SX-3192.' },
        ],
    },
    config: {
        title: 'Describe your project',
        blurb: 'Tell A3 your objective in plain language — it scaffolds the missions, workflows and Zod schemas from scratch.',
        items: [
            { icon: 'far fa-wand-magic-sparkles', label: 'Apply for a job', desc: 'Set up an agent to apply for jobs on the careers site for me. It should search for matching roles, open a role, fill in my personal details and work experience, upload my resume, review the application, and submit it.', prompt: 'Set up an agent to apply for jobs on the careers site for me. It should search for matching roles, open a role, fill in my personal details and work experience, upload my resume, review the application, and submit it.' },
        ],
    },
};

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 };
