/** Structural mirror of collector/src/adapters/factory.ts panel payload. */

export interface FactoryEventView {
  rowid: number
  eventId: string
  phaseId: string | null
  parentId: string | null
  type: string | null
  name: string | null
  payloadJson: string | null
  tokens: number | null
  startedAt: string | null
  endedAt: string | null
}

export interface FactoryAgentAttemptView {
  attemptId: string
  phaseId: string | null
  agent: string | null
  sessionId: string | null
  command: string | null
  systemPrompt: string | null
  userPrompt: string | null
  returncode: number | null
  signal: number | null
  timedOut: boolean
  tokens: number | null
  usage: FactoryUsageView | null
  providerFailure: FactoryProviderFailureView | null
  error: string | null
  stderrPath: string | null
  host: string | null
  account: string | null
  model: string | null
  startedAt: string | null
  lastOutputAt: string | null
  endedAt: string | null
  durationMs: number | null
  idleMs: number | null
}

export interface FactoryProviderFailureView {
  kind: 'capped' | 'unavailable' | 'protocol' | 'context_overflow' | 'timeout' | 'cancelled'
  detail: string
  retryAfterSeconds?: number | null
  resumeAt?: string | null
}

export interface FactoryUsageView {
  inputTokens: number | null
  outputTokens: number | null
  cacheReadTokens: number | null
  cacheWriteTokens: number | null
  reasoningTokens: number | null
  totalTokens: number | null
  inputCost: number | null
  outputCost: number | null
  cacheReadCost: number | null
  cacheWriteCost: number | null
  totalCost: number | null
  usageEstimated: boolean
  billingStatus: string | null
  maxTokens: number | null
  contextWindow: number | null
  providerFailure?: FactoryProviderFailureView | null
}

export interface FactoryGateResultView {
  adwId: string
  phaseId: string | null
  attempt: number | null
  gate: string | null
  passed: boolean | null
  checks: unknown
}

export interface FactoryPhaseView {
  phaseId: string
  seq: number | null
  name: string | null
  kind: string | null
  owner: string | null
  description: string | null
  status: string | null
  attempt: number | null
  retries: number | null
  error: string | null
  startedAt: string | null
  endedAt: string | null
  durationMs?: number | null
  tokens: number
  spend: number
}

export interface FactoryDecisionOptionView {
  value: string
  label: string
  recommended?: boolean
}

export interface FactoryDecisionView {
  decisionId: string
  phase: string | null
  question: string
  options: FactoryDecisionOptionView[]
  freeText: boolean
  context: string
  status: string
  answerValue: string | null
  answerText: string | null
  answeredBy: string | null
  createdAt: string
  answeredAt: string | null
}

export interface FactoryDiffFileView {
  path: string
  status: string | null
  insertions: number | null
  deletions: number | null
}

export interface FactoryPhaseDiffView {
  phaseId: string
  attempt: number | null
  files: FactoryDiffFileView[]
  insertions: number | null
  deletions: number | null
  diffText: string | null
  truncated: boolean
  createdAt: string | null
}

export interface FactoryProcessView {
  kind: string | null
  name: string | null
  pid: number | null
  command: string | null
  startedAt: string | null
  endedAt: string | null
  durationMs: number | null
}

export interface FactoryRunView {
  adwId: string
  repo: string | null
  repoName: string | null
  adwName: string | null
  runSlug: string | null
  preset?: string | null
  request: string | null
  status: string | null
  engineer: string | null
  startedAt: string | null
  endedAt: string | null
  totalTokens: number | null
  totalCost: number | null
  detailAvailable?: boolean
  phases: FactoryPhaseView[]
  events: FactoryEventView[]
  attempts?: FactoryAgentAttemptView[]
  gates?: FactoryGateResultView[]
  diffs?: FactoryPhaseDiffView[]
  processes?: FactoryProcessView[]
  unavailableTables?: string[]
  decisions: FactoryDecisionView[]
}

export interface FactoryPanelData {
  dbPath: string | null
  dbPresent: boolean
  runs: FactoryRunView[]
}
