import type { Panel } from '../../src/lib/collector-types'
import type { SessionView, SessionsPanelData } from '../../src/lib/session-types'

/**
 * TEST-ONLY session records. Every branch of the agent-session contract is represented:
 * each cli, each launchedBy, each state, each attach kind, rows with fields absent, many
 * projects, and one deliberately broken timestamp.
 *
 * NEVER import this from `src/` — a fixture row rendered to a real user is exactly the
 * fabricated data this panel exists to avoid. `sessions-fixtures.test.ts` enforces it.
 */

const BASE: SessionView = {
  id: 'base',
  runtime: 'claude',
  pid: 4242,
  cwd: '/home/user/Projects/overdeck',
  repo: 'overdeck',
  repoRoot: '/home/user/Projects/overdeck',
  branch: 'main',
  worktree: null,
  host: 'e14',
  startedAt: '2026-08-07T00:00:00.000Z',
  lastHeartbeatAt: '2026-08-07T02:00:00.000Z',
  lastProgressAt: '2026-08-07T02:00:00.000Z',
  lastActiveAt: '2026-08-07T02:00:00.000Z',
  finishedAt: null,
  finishReason: null,
  transcriptPath: '/home/user/.claude/projects/overdeck/base.jsonl',
  tmuxSession: null,
  tmuxSocket: null,
  mux: null,
  attached: false,
  evidence: null,
  parent: null,
  parentLedgerId: null,
  state: 'ALIVE-WORKING',
  resumeId: null,
  launchedBy: null,
  title: null,
  activity: null,
  account: null,
  project: '/home/user/Projects/overdeck',
  hostFacts: { name: 'e14', reachability: 'unknown', state: null, notes: null },
  attach: { kind: 'none', reason: 'fixture default' },
  attachable: false,
}

function session(overrides: Partial<SessionView>): SessionView {
  return { ...BASE, ...overrides }
}

export const SESSION_FIXTURES: SessionView[] = [
  // claude, started by the user, live in tmux, screen renderable in the browser.
  session({
    id: 'sess-tmux-live',
    title: 'wire the observability panel',
    activity: 'running collector tests',
    launchedBy: 'user',
    branch: 'wt/obs-panel',
    worktree: '/home/user/Projects/overdeck/.worktrees/obs-panel',
    repoRoot: '/home/user/Projects/overdeck/.worktrees/obs-panel',
    project: '/home/user/Projects/overdeck/.worktrees/obs-panel',
    tmuxSession: 'agent-sess-tmux-live',
    tmuxSocket: '/home/user/.local/state/human-session/tmux.sock',
    attached: true,
    evidence: 'cpu ticks accumulating',
    state: 'ALIVE-WORKING',
    resumeId: '32370ce3-3b07-4b12-9e8a-9d6e21188bce',
    attach: { kind: 'tmux', target: 'agent-sess-tmux-live', command: 'agent-sessions attach sess-tmux-live' },
    attachable: true,
  }),
  // claude launched by another agent, dtach-hosted: reopens from a terminal, no browser screen.
  session({
    id: 'sess-dtach-detached',
    title: 'close the ledger enrollment gap',
    activity: 'editing agent-session-ledger.sh',
    launchedBy: 'agent',
    parent: 'ledger enrollment agent',
    parentLedgerId: 'claude-20260807T055511Z-311635',
    mux: { kind: 'dtach', socket: '/home/user/.local/state/agent-sessions/sock/sess-dtach-detached', target: 'sess-dtach-detached' },
    state: 'DETACHED-ALIVE',
    attached: false,
    evidence: 'running with no window attached',
    lastProgressAt: '2026-08-07T02:40:00.000Z',
    lastActiveAt: '2026-08-07T02:40:00.000Z',
    attach: { kind: 'tmux', target: 'sess-dtach-detached', command: 'agent-sessions attach sess-dtach-detached' },
    attachBlockedReason: 'this session is not hosted in tmux, so its screen cannot be rendered in the browser',
  }),
  // factory-launched codex on another project, idle: the wedged-agent case.
  session({
    id: 'sess-codex-idle',
    runtime: 'codex',
    title: 'multideal migration re-baseline',
    activity: null,
    launchedBy: 'factory',
    cwd: '/home/user/Projects/multideal',
    repo: 'multideal',
    repoRoot: '/home/user/Projects/multideal',
    project: '/home/user/Projects/multideal',
    branch: 'wt/drizzle-rebaseline',
    state: 'ALIVE-IDLE',
    attached: true,
    evidence: 'no progress for 41m',
    lastProgressAt: '2026-08-07T01:20:00.000Z',
    lastActiveAt: '2026-08-07T01:20:00.000Z',
    mux: { kind: 'dtach', socket: '/home/user/.local/state/agent-sessions/sock/sess-codex-idle', target: 'sess-codex-idle' },
    attach: { kind: 'tmux', target: 'sess-codex-idle', command: 'agent-sessions attach sess-codex-idle' },
    attachBlockedReason: 'this session is not hosted in tmux, so its screen cannot be rendered in the browser',
  }),
  // cursor-agent, finished, resumes by its own verb on a third project.
  session({
    id: 'sess-cursor-finished',
    runtime: 'cursor-agent',
    title: 'restyle the limits page',
    launchedBy: 'user',
    cwd: '/home/user/Projects/invariantum',
    repo: 'invariantum',
    repoRoot: '/home/user/Projects/invariantum',
    project: '/home/user/Projects/invariantum',
    branch: 'main',
    state: 'FINISHED',
    startedAt: '2026-08-06T19:00:00.000Z',
    finishedAt: '2026-08-06T21:00:00.000Z',
    lastProgressAt: '2026-08-06T21:00:00.000Z',
    lastActiveAt: '2026-08-06T21:00:00.000Z',
    lastHeartbeatAt: '2026-08-06T21:00:00.000Z',
    attached: false,
    resumeId: 'chat-8812',
    attach: { kind: 'resume', target: 'chat-8812', command: 'cursor-agent --resume chat-8812' },
    attachBlockedReason: 'session is FINISHED',
  }),
  // claude, finished, account recorded — the account-aware resume command.
  session({
    id: 'sess-claude-finished',
    title: 'security-gate prompt lever',
    launchedBy: 'user',
    cwd: '/home/user/Projects/security-gate',
    repo: 'security-gate',
    repoRoot: '/home/user/Projects/security-gate',
    project: '/home/user/Projects/security-gate',
    branch: 'main',
    state: 'FINISHED',
    startedAt: '2026-08-06T17:00:00.000Z',
    finishedAt: '2026-08-06T18:30:00.000Z',
    finishReason: 'cleared by the user',
    lastProgressAt: '2026-08-06T18:30:00.000Z',
    lastActiveAt: '2026-08-06T18:30:00.000Z',
    lastHeartbeatAt: '2026-08-06T18:30:00.000Z',
    account: 'zync2',
    resumeId: 'c0c245b9-d222-46a3-b6e9-d16f2626c5f9',
    attach: { kind: 'resume', target: 'c0c245b9-d222-46a3-b6e9-d16f2626c5f9', command: 'cld --account zync2 --resume c0c245b9-d222-46a3-b6e9-d16f2626c5f9' },
    attachBlockedReason: 'session is FINISHED',
  }),
  // Orphaned with nothing to resume: the honest dead end.
  session({
    id: 'sess-orphaned-noresume',
    launchedBy: 'user',
    cwd: '/home/user/Projects/virtuac',
    repo: 'virtuac',
    repoRoot: '/home/user/Projects/virtuac',
    project: '/home/user/Projects/virtuac',
    branch: 'wt/auth-blocker',
    pid: null,
    state: 'ORPHANED',
    startedAt: '2026-08-06T13:00:00.000Z',
    attached: false,
    evidence: 'no process carries this session id; its terminal is gone too',
    lastProgressAt: '2026-08-06T15:00:00.000Z',
    lastActiveAt: '2026-08-06T15:00:00.000Z',
    lastHeartbeatAt: '2026-08-06T15:00:00.000Z',
    attach: { kind: 'none', reason: 'no session id was recorded for this claude session, so it cannot be resumed by id' },
    attachBlockedReason: 'session is ORPHANED',
  }),
  // Closed by the sweep with nothing behind it — the row exists only to say so.
  session({
    id: 'sess-nothing-recorded',
    cwd: '/home/user/Projects/harnessd',
    repo: 'harnessd',
    repoRoot: '/home/user/Projects/harnessd',
    project: '/home/user/Projects/harnessd',
    branch: null,
    pid: null,
    transcriptPath: null,
    state: 'FINISHED',
    startedAt: '2026-08-06T12:00:00.000Z',
    finishedAt: '2026-08-06T12:00:20.000Z',
    finishReason: 'process exited, nothing was recorded to recover',
    lastProgressAt: null,
    lastActiveAt: '2026-08-06T12:00:20.000Z',
    lastHeartbeatAt: '2026-08-06T12:00:20.000Z',
    attached: false,
    attach: { kind: 'none', reason: 'no session id was recorded for this claude session, so it cannot be resumed by id' },
    attachBlockedReason: 'session is FINISHED',
  }),
  // Off-host on the one reachable buildbox: reconnects over ssh, never rendered here.
  session({
    id: 'sess-remote-debian1',
    title: 'playwright shard 3',
    launchedBy: 'agent',
    parentLedgerId: 'claude-20260807T055620Z-428807',
    host: 'debian1',
    hostFacts: { name: 'debian1', reachability: 'declared-enabled', state: 'reachable', notes: 'kvm; e2e target' },
    cwd: '/home/user/builds/overdeck',
    repo: 'overdeck',
    repoRoot: '/home/user/builds/overdeck',
    project: '/home/user/builds/overdeck',
    mux: { kind: 'dtach', socket: '/run/sock/sess-remote-debian1', target: 'sess-remote-debian1' },
    attach: { kind: 'ssh', target: 'debian1', command: 'ssh debian1 agent-sessions attach sess-remote-debian1' },
    attachBlockedReason: 'this session runs on debian1; only e14 screens can be rendered here',
  }),
  // Bricked host, known only from the static registry. NOTHING may probe it.
  session({
    id: 'sess-remote-debian3',
    launchedBy: null,
    host: 'debian3',
    hostFacts: { name: 'debian3', reachability: 'declared-disabled', state: 'bricked', notes: 'bricked 2026-08-07 — never route work here' },
    cwd: '/home/user/builds/overdeck',
    repoRoot: '/home/user/builds/overdeck',
    project: '/home/user/builds/overdeck',
    state: 'ORPHANED',
    startedAt: '2026-08-06T08:00:00.000Z',
    attached: null,
    lastProgressAt: '2026-08-06T09:00:00.000Z',
    lastActiveAt: '2026-08-06T09:00:00.000Z',
    lastHeartbeatAt: '2026-08-06T09:00:00.000Z',
    attach: { kind: 'none', reason: 'host debian3 is disabled in the buildbox registry (bricked 2026-08-07 — never route work here)' },
    attachBlockedReason: 'this session runs on debian3; only e14 screens can be rendered here',
  }),
  // Everything optional absent: no title, no activity, no launcher, no host, no timestamps.
  session({
    id: 'sess-bare',
    host: null,
    hostFacts: null,
    branch: null,
    startedAt: null,
    lastHeartbeatAt: null,
    lastProgressAt: null,
    lastActiveAt: null,
    attached: null,
    cwd: '/home/user/Projects/scratch',
    repo: null,
    repoRoot: '/home/user/Projects/scratch',
    project: '/home/user/Projects/scratch',
    state: 'ALIVE-IDLE',
    attach: { kind: 'none', reason: 'this session was started without a multiplexer, so there is no window to reattach to' },
    attachBlockedReason: 'this session is not hosted in tmux, so its screen cannot be rendered in the browser',
  }),
  // Broken clock: a start time in the future. Ages must read unknown, never negative.
  session({
    id: 'sess-bad-clock',
    title: 'entry written with a local time labelled Z',
    launchedBy: 'user',
    startedAt: '2099-01-01T00:00:00.000Z',
    lastProgressAt: '2099-01-01T00:00:00.000Z',
    lastActiveAt: '2099-01-01T00:00:00.000Z',
    lastHeartbeatAt: '2099-01-01T00:00:00.000Z',
    cwd: '/home/user/Projects/harnessd',
    repo: 'harnessd',
    repoRoot: '/home/user/Projects/harnessd',
    project: '/home/user/Projects/harnessd',
    state: 'ALIVE-WORKING',
    mux: { kind: 'dtach', socket: '/run/sock/sess-bad-clock', target: 'sess-bad-clock' },
    attach: { kind: 'tmux', target: 'sess-bad-clock', command: 'agent-sessions attach sess-bad-clock' },
    attachBlockedReason: 'this session is not hosted in tmux, so its screen cannot be rendered in the browser',
  }),
  // Started outside any checkout: the repo walk lands on the home directory, which must
  // never render as a project named after the user.
  session({
    id: 'sess-home-root',
    launchedBy: 'user',
    cwd: '/home/user',
    repo: null,
    repoRoot: '/home/user',
    project: '/home/user',
    branch: null,
    state: 'ALIVE-IDLE',
    lastProgressAt: '2026-08-07T01:55:00.000Z',
    lastActiveAt: '2026-08-07T01:55:00.000Z',
    attach: { kind: 'none', reason: 'this session was started without a multiplexer, so there is no window to reattach to' },
    attachBlockedReason: 'this session is not hosted in tmux, so its screen cannot be rendered in the browser',
  }),
]

export const SESSIONS_PANEL_DATA: SessionsPanelData = {
  ledgerDir: '/home/user/.local/state/agent-sessions',
  ledgerMissing: false,
  unreadableFiles: [],
  tmuxAvailable: true,
  attachEnabled: true,
  localHost: 'e14',
  homeDir: '/home/user',
  hosts: [
    { name: 'debian1', reachability: 'declared-enabled', state: 'reachable', notes: 'kvm; e2e target' },
    { name: 'debian3', reachability: 'declared-disabled', state: 'bricked', notes: 'bricked 2026-08-07 — never route work here' },
  ],
  hostsRegistryPath: '/home/user/.claude/buildbox-hosts.json',
  hostsRegistryMissing: false,
  hostProbes: [
    { host: 'debian1', status: 'ok', error: null, at: '2026-08-07T02:00:00.000Z', sessionCount: 2 },
    { host: 'debian3', status: 'not-reachable', error: 'registry state: bricked', at: '2026-08-07T02:00:00.000Z', sessionCount: 0 },
  ],
  sessions: SESSION_FIXTURES,
}

export const SESSIONS_PANELS: Panel[] = [
  { id: 'sessions', ts: '2026-08-07T02:00:05.000Z', data: SESSIONS_PANEL_DATA },
]
