// ── Base entry (every JSONL line has these) ──

export interface TranscriptEntryBase {
  uuid: string
  parentUuid: string | null
  type: string
  timestamp: string
  sessionId?: string
  isSidechain?: boolean
  userType?: string
  cwd?: string
  version?: string
  gitBranch?: string
}

// ── Specific entry types ──

export interface UserEntry extends TranscriptEntryBase {
  type: 'user'
  message: {
    role: 'user'
    content: string | ContentBlock[]
  }
  isMeta?: boolean
}

export interface AssistantEntry extends TranscriptEntryBase {
  type: 'assistant'
  message: {
    id: string
    model: string
    role: 'assistant'
    type: 'message'
    content: ContentBlock[]
    usage?: {
      input_tokens: number
      output_tokens: number
      cache_read_input_tokens?: number
      cache_creation_input_tokens?: number
    }
    stop_reason?: string
  }
}

export interface ProgressEntry extends TranscriptEntryBase {
  type: 'progress'
  data: {
    type: string // 'hook_progress', 'tool_progress', 'mcp_progress'
    hookEvent?: string
    hookName?: string
    command?: string
    toolName?: string
    [key: string]: unknown
  }
  toolUseID?: string
  parentToolUseID?: string
}

export interface SystemEntry extends TranscriptEntryBase {
  type: 'system'
  message?: {
    role: 'user'
    content: string | ContentBlock[]
  }
  subtype?: string
}

export interface FileHistoryEntry {
  type: 'file-history-snapshot'
  messageId: string
  snapshot: {
    messageId: string
    trackedFileBackups: Record<string, unknown>
    timestamp: string
  }
  isSnapshotUpdate: boolean
}

// ── Content blocks (inside messages) ──

export type ContentBlock =
  | TextBlock
  | ThinkingBlock
  | ToolUseBlock
  | ToolResultBlock

export interface TextBlock {
  type: 'text'
  text: string
}

export interface ThinkingBlock {
  type: 'thinking'
  thinking: string
}

export interface ToolUseBlock {
  type: 'tool_use'
  id: string
  name: string
  input: Record<string, unknown>
}

export interface ToolResultBlock {
  type: 'tool_result'
  tool_use_id: string
  content: string | ContentBlock[]
  is_error?: boolean
}

// ── Parsed conversation (what the UI renders) ──

export type AgentName = 'ejecutor' | 'nucleo000' | 'claude'

export interface SessionInfo {
  id: string
  filename: string
  size: number
  mtime: number
  mtimeISO: string
  firstUserMessage: string
}

export interface ParsedConversation {
  sessionId: string
  model: string
  version: string
  messages: ConversationMessage[]
  metadata: ConversationMetadata
}

export interface ConversationMetadata {
  cwd: string
  gitBranch: string
  startTime: string
  endTime: string
  totalEntries: number
}

export type ConversationMessage =
  | ParsedUserMessage
  | ParsedAssistantMessage
  | ParsedProgressGroup
  | ParsedSystemMessage

export interface ParsedUserMessage {
  kind: 'user'
  uuid: string
  timestamp: string
  content: string
  isMeta: boolean
  isToolResult: boolean
  toolResults: ParsedToolResult[]
}

export interface ParsedToolResult {
  toolUseId: string
  content: string
  isError: boolean
}

export interface ParsedAssistantMessage {
  kind: 'assistant'
  uuid: string
  messageId: string
  timestamp: string
  model: string
  textBlocks: string[]
  thinkingBlocks: string[]
  toolCalls: ParsedToolCall[]
  usage?: {
    input_tokens: number
    output_tokens: number
    cache_read_input_tokens?: number
    cache_creation_input_tokens?: number
  }
  stopReason?: string
}

export interface ParsedToolCall {
  id: string
  name: string
  input: Record<string, unknown>
  result?: ParsedToolResult
  progressEvents: ParsedProgressEvent[]
}

export interface ParsedProgressEvent {
  uuid: string
  timestamp: string
  dataType: string // 'hook_progress' | 'mcp_progress'
  // hook_progress fields
  hookEvent?: string // 'SessionStart' | 'PreToolUse' | 'PostToolUse'
  hookName?: string
  command?: string
  // mcp_progress fields
  mcpStatus?: string // 'started' | 'completed'
  mcpServerName?: string
  mcpToolName?: string
  mcpElapsedMs?: number
  data: Record<string, unknown>
}

export interface ParsedProgressGroup {
  kind: 'progress'
  uuid: string
  timestamp: string
  events: ParsedProgressEvent[]
}

export interface ParsedSystemMessage {
  kind: 'system'
  uuid: string
  timestamp: string
  content: string
  subtype?: string
  durationMs?: number
}

export interface SectionSummary {
  total: number
  assistantCount: number
  systemCount: number
  progressCount: number
  toolNames: string[]
  hasErrors: boolean
  errorCount: number
  inputTokens: number
  outputTokens: number
}

// ── Terminal slot (persistent terminal registry) ──

export interface TerminalSlot {
  sessionId: string           // Claude transcript session ID (transcriptSessionId)
  ephemeralSessionId: string  // PTY session ID on server (for reconnection)
  agent: AgentName
  label: string               // firstUserMessage (truncated) or "New session"
  command: string             // The command used to start
  active: boolean             // Is this the currently displayed terminal (for this client)?
  alive: boolean              // PTY exists on server
  clients: number             // Number of connected WS clients
}

// ── Raw entry union ──

export type TranscriptEntry =
  | UserEntry
  | AssistantEntry
  | ProgressEntry
  | SystemEntry
  | FileHistoryEntry
  | (TranscriptEntryBase & { type: string })
