// Session + message fixtures for ChatTerm.
// Output is modeled as a stream of "lines" with type + ansi-style color hints.
// Types: out (stdout), err (stderr), cmd (user input), sys (system), ask (agent question), done (agent finished),
//        tool (tool-call banner), diff (code diff), prog (progress bar line).

const now = Date.now();
const m = (mins) => now - mins * 60_000;
const s = (secs) => now - secs * 1000;

const SESSIONS = [
  {
    id: "claude-refactor",
    name: "claude-code · auth refactor",
    short: "Auth Refactor",
    kind: "agent",
    avatar: { mono: "AR", color: "var(--av-3)" },
    status: "done", // just finished — top of list
    unread: 4,
    pinned: false,
    muted: false,
    lastActive: s(12),
    lastPreview: "✓ All 47 tests passing. Refactor complete.",
    lastSender: "claude-code",
    cwd: "~/work/atlas-api",
    branch: "feat/auth-refactor",
    model: "claude-sonnet-4.5",
    tokens: { in: 142_300, out: 28_900 },
    tools: [
      { name: "read_file", count: 38 },
      { name: "edit_file", count: 14 },
      { name: "run_shell", count: 9 },
      { name: "grep", count: 22 },
    ],
    duration: "14m 22s",
    lines: [
      { t: "sys", text: "claude-code session started · model: sonnet-4.5 · cwd: ~/work/atlas-api" },
      { t: "cmd", text: "refactor src/auth/** to use the new TokenProvider interface, keep tests green" },
      { t: "out", text: "" },
      { t: "tool", tool: "grep", args: "\"TokenProvider\" in src/", text: "" },
      { t: "out", text: "Found 7 references across 4 files." },
      { t: "tool", tool: "read_file", args: "src/auth/session.ts", text: "" },
      { t: "out", text: "Reading src/auth/session.ts (213 lines)…" },
      { t: "out", text: "" },
      { t: "agent", text: "I'll migrate session.ts first — it's the narrowest surface. Plan:" },
      { t: "agent", text: "  1. Replace LegacyToken with TokenProvider in the class generics" },
      { t: "agent", text: "  2. Inline the refresh() shim since new interface handles it" },
      { t: "agent", text: "  3. Update the 3 call-sites in middleware/" },
      { t: "tool", tool: "edit_file", args: "src/auth/session.ts", text: "" },
      { t: "diff", text: "─ src/auth/session.ts" , diff: [
        { kind: "rem", text: "-import { LegacyToken } from './legacy';" },
        { kind: "add", text: "+import { TokenProvider } from './token-provider';" },
        { kind: "ctx", text: " " },
        { kind: "rem", text: "-class Session<T extends LegacyToken> {" },
        { kind: "add", text: "+class Session<T extends TokenProvider> {" },
        { kind: "ctx", text: "   constructor(private token: T) {}" },
        { kind: "rem", text: "-  async refresh() { return this.token.refreshLegacy(); }" },
        { kind: "add", text: "+  async refresh() { return this.token.refresh(); }" },
      ]},
      { t: "tool", tool: "run_shell", args: "pnpm test src/auth", text: "" },
      { t: "prog", label: "Test suite", pct: 100, detail: "47 passed, 0 failed · 3.2s" },
      { t: "out", text: "", color: "green" },
      { t: "out", text: "✓ src/auth/session.test.ts (12)", color: "green" },
      { t: "out", text: "✓ src/auth/middleware.test.ts (21)", color: "green" },
      { t: "out", text: "✓ src/auth/token-provider.test.ts (14)", color: "green" },
      { t: "done", text: "✓ All 47 tests passing. Refactor complete.", summary: {
        filesChanged: 4, insertions: 38, deletions: 62, testsPassed: 47, testsFailed: 0
      }},
    ],
  },
  {
    id: "dev-server",
    name: "npm run dev · atlas-api",
    short: "dev server",
    kind: "process",
    avatar: { mono: "DV", color: "var(--av-1)" },
    status: "running",
    unread: 0,
    pinned: true,
    muted: false,
    lastActive: s(2),
    lastPreview: "compiled client in 284ms",
    lastSender: "vite",
    cwd: "~/work/atlas-api",
    branch: "feat/auth-refactor",
    pid: 48221,
    uptime: "2h 14m",
    port: 5173,
    lines: [
      { t: "cmd", text: "pnpm run dev" },
      { t: "out", text: "" },
      { t: "out", text: "  VITE v5.4.10  ready in 412 ms", color: "bright-green" },
      { t: "out", text: "" },
      { t: "out", text: "  ➜  Local:   http://localhost:5173/", color: "gray" },
      { t: "out", text: "  ➜  Network: use --host to expose", color: "gray" },
      { t: "out", text: "" },
      { t: "out", text: "[14:32:18] hmr update /src/auth/session.ts", color: "cyan" },
      { t: "out", text: "[14:32:18] hmr update /src/auth/middleware.ts", color: "cyan" },
      { t: "out", text: "[14:32:21] page reload src/main.tsx", color: "yellow" },
      { t: "out", text: "[14:34:02] hmr update /src/auth/token-provider.ts", color: "cyan" },
      { t: "out", text: "[14:34:47] compiled client in 284ms", color: "bright-green" },
    ],
  },
  {
    id: "ssh-prod",
    name: "ssh · prod-web-01",
    short: "prod-web-01",
    kind: "ssh",
    avatar: { mono: "P1", color: "var(--av-8)" },
    status: "waiting",
    unread: 1,
    pinned: true,
    muted: false,
    lastActive: m(4),
    lastPreview: "$ ",
    lastSender: "you",
    cwd: "/var/log/atlas",
    host: "prod-web-01.atlas.internal",
    user: "deploy",
    uptime: "47 days",
    lines: [
      { t: "sys", text: "Connected to prod-web-01.atlas.internal (10.0.4.22)" },
      { t: "sys", text: "Last login: Thu Apr 17 09:14:22 2026 from 192.168.1.104" },
      { t: "out", text: "deploy@prod-web-01:~$ ", color: "bright-green", raw: true },
      { t: "cmd", text: "cd /var/log/atlas && tail -n 20 app.log", inline: true },
      { t: "out", text: "" },
      { t: "out", text: "2026-04-18T14:28:01Z INFO  [http] GET /v2/sessions · 12ms · 200", color: "gray" },
      { t: "out", text: "2026-04-18T14:28:03Z INFO  [http] POST /v2/auth/refresh · 41ms · 200", color: "gray" },
      { t: "out", text: "2026-04-18T14:28:07Z WARN  [auth] rate-limit near threshold · user=u_48a2", color: "yellow" },
      { t: "out", text: "2026-04-18T14:28:14Z INFO  [http] GET /v2/feed · 88ms · 200", color: "gray" },
      { t: "out", text: "2026-04-18T14:28:22Z ERROR [db]   pool exhausted · queued=17", color: "red" },
      { t: "out", text: "2026-04-18T14:28:22Z ERROR [db]   pool exhausted · queued=18", color: "red" },
      { t: "out", text: "2026-04-18T14:28:23Z INFO  [db]   pool recovered · active=24/32", color: "gray" },
      { t: "out", text: "" },
      { t: "out", text: "deploy@prod-web-01:/var/log/atlas$ ", color: "bright-green", raw: true },
    ],
  },
  {
    id: "agent-group-migration",
    name: "Agent Group · DB Migration",
    short: "db migration",
    kind: "group",
    avatar: { mono: "MG", color: "var(--av-2)", group: true },
    members: [
      { mono: "PL", color: "var(--av-4)", role: "planner" },
      { mono: "EX", color: "var(--av-5)", role: "executor" },
      { mono: "RV", color: "var(--av-7)", role: "reviewer" },
    ],
    status: "running",
    unread: 12,
    pinned: false,
    muted: false,
    lastActive: s(28),
    lastPreview: "executor: applying 003_add_token_table.sql…",
    lastSender: "executor",
    cwd: "~/work/atlas-infra",
    lines: [
      { t: "sys", text: "Agent group initialized · 3 agents: planner, executor, reviewer" },
      { t: "cmd", text: "@planner migrate the atlas DB to add token_providers table" },
      { t: "agent", who: "planner", color: "var(--av-4)", text: "Breaking this into 3 migrations:" },
      { t: "agent", who: "planner", color: "var(--av-4)", text: "  001_drop_legacy_tokens.sql — reversible drop" },
      { t: "agent", who: "planner", color: "var(--av-4)", text: "  002_create_token_providers.sql — new table" },
      { t: "agent", who: "planner", color: "var(--av-4)", text: "  003_backfill_tokens.sql — copy data from legacy" },
      { t: "agent", who: "planner", color: "var(--av-4)", text: "→ handing off to @executor" },
      { t: "agent", who: "executor", color: "var(--av-5)", text: "Running 001_drop_legacy_tokens.sql on staging…" },
      { t: "out", text: "DROP TABLE", color: "green" },
      { t: "agent", who: "executor", color: "var(--av-5)", text: "Running 002_create_token_providers.sql on staging…" },
      { t: "out", text: "CREATE TABLE", color: "green" },
      { t: "agent", who: "reviewer", color: "var(--av-7)", text: "Staging schema looks good. Ready for 003." },
      { t: "agent", who: "executor", color: "var(--av-5)", text: "applying 003_add_token_table.sql…" },
      { t: "prog", label: "Backfilling tokens", pct: 62, detail: "1.4M / 2.3M rows · eta 40s" },
    ],
  },
  {
    id: "docker-logs",
    name: "docker logs · postgres",
    short: "postgres",
    kind: "process",
    avatar: { mono: "PG", color: "var(--av-6)" },
    status: "running",
    unread: 0,
    pinned: false,
    muted: true,
    lastActive: s(45),
    lastPreview: "LOG: checkpoint complete",
    lastSender: "postgres",
    cwd: "—",
    container: "atlas-postgres-1",
    image: "postgres:16.2",
    lines: [
      { t: "out", text: "2026-04-18 14:30:02.114 UTC [1] LOG:  database system is ready to accept connections", color: "gray" },
      { t: "out", text: "2026-04-18 14:32:14.882 UTC [82] LOG:  checkpoint starting: time", color: "gray" },
      { t: "out", text: "2026-04-18 14:34:01.221 UTC [82] LOG:  checkpoint complete: wrote 412 buffers (2.5%)", color: "gray" },
    ],
  },
  {
    id: "build-ci",
    name: "CI · main · build #2,148",
    short: "CI #2148",
    kind: "ci",
    avatar: { mono: "CI", color: "var(--av-7)" },
    status: "error",
    unread: 3,
    pinned: false,
    muted: false,
    lastActive: m(8),
    lastPreview: "✗ FAILED · type-check · 2 errors in src/feed/",
    lastSender: "github-actions",
    cwd: "—",
    repo: "atlas/atlas-api",
    commit: "a3f82c1",
    author: "you",
    lines: [
      { t: "sys", text: "github-actions · workflow: ci.yml · trigger: push to main" },
      { t: "out", text: "[install] pnpm install --frozen-lockfile · 12.4s", color: "gray" },
      { t: "out", text: "[lint] eslint . · 4.1s · 0 errors", color: "green" },
      { t: "out", text: "[type-check] tsc --noEmit · 6.2s", color: "gray" },
      { t: "err", text: "src/feed/normalize.ts:42:18 - error TS2345: Argument of type 'FeedItem | undefined'" },
      { t: "err", text: "  is not assignable to parameter of type 'FeedItem'." },
      { t: "err", text: "src/feed/paginate.ts:88:7 - error TS2322: Type 'string' is not assignable to 'Cursor'." },
      { t: "done", text: "✗ FAILED · type-check · 2 errors in src/feed/", success: false },
    ],
  },
  {
    id: "codex-tests",
    name: "codex · write feed tests",
    short: "feed tests",
    kind: "agent",
    avatar: { mono: "FT", color: "var(--av-4)" },
    status: "waiting",
    unread: 2,
    pinned: false,
    muted: false,
    lastActive: m(6),
    lastPreview: "Should I mock the Redis client or use testcontainers?",
    lastSender: "codex",
    cwd: "~/work/atlas-api",
    model: "codex-large",
    lines: [
      { t: "sys", text: "codex session · model: codex-large · cwd: ~/work/atlas-api" },
      { t: "cmd", text: "write integration tests for src/feed/paginate.ts" },
      { t: "tool", tool: "read_file", args: "src/feed/paginate.ts", text: "" },
      { t: "tool", tool: "read_file", args: "src/feed/paginate.test.ts", text: "" },
      { t: "agent", text: "The existing tests mock the DB but not Redis. Before I scaffold the new ones —" },
      { t: "ask", text: "Should I mock the Redis client or use testcontainers?" , choices: ["Mock it", "Use testcontainers", "Let me decide later"]},
    ],
  },
  {
    id: "shell-local",
    name: "zsh · ~/work",
    short: "local shell",
    kind: "shell",
    avatar: { mono: "$", color: "var(--av-1)" },
    status: "idle",
    unread: 0,
    pinned: false,
    muted: false,
    lastActive: m(22),
    lastPreview: "$ git status",
    lastSender: "you",
    cwd: "~/work",
    branch: "—",
    lines: [
      { t: "out", text: "~/work ❯ ", color: "bright-green", raw: true },
      { t: "cmd", text: "git status", inline: true },
      { t: "out", text: "On branch feat/auth-refactor" },
      { t: "out", text: "Your branch is up to date with 'origin/feat/auth-refactor'." },
      { t: "out", text: "nothing to commit, working tree clean" },
      { t: "out", text: "~/work ❯ ", color: "bright-green", raw: true },
    ],
  },
  {
    id: "git-hook",
    name: "git-hook · pre-push",
    short: "pre-push",
    kind: "hook",
    avatar: { mono: "GH", color: "var(--av-5)" },
    status: "done",
    unread: 0,
    pinned: false,
    muted: false,
    lastActive: m(15),
    lastPreview: "✓ pre-push checks passed in 8.1s",
    lastSender: "husky",
    cwd: "~/work/atlas-api",
    lines: [
      { t: "sys", text: "husky · pre-push hook" },
      { t: "out", text: "› lint-staged · 3.2s · ok", color: "green" },
      { t: "out", text: "› type-check · 4.9s · ok", color: "green" },
      { t: "done", text: "✓ pre-push checks passed in 8.1s", success: true },
    ],
  },
  {
    id: "ssh-staging",
    name: "ssh · staging-db",
    short: "staging-db",
    kind: "ssh",
    avatar: { mono: "SD", color: "var(--av-2)" },
    status: "idle",
    unread: 0,
    pinned: false,
    muted: false,
    lastActive: m(48),
    lastPreview: "Connection closed.",
    lastSender: "system",
    cwd: "—",
    host: "staging-db.atlas.internal",
    lines: [
      { t: "sys", text: "Connection closed." },
    ],
  },
];

// Reactions are keyed by (sessionId, lineIdx) -> {emoji: count}
const INITIAL_REACTIONS = {
  "claude-refactor:21": { "👍": 1 },
  "ssh-prod:7": { "🐛": 1 },
  "build-ci:5": { "🐛": 2 },
};

Object.assign(window, { SESSIONS, INITIAL_REACTIONS });
