// Beaver frontend types

export interface AuthUser {
  id: string;
  username: string;
  email: string;
  role: string;
  quota_tier: string;
}

export interface BackendConnectionInfo {
  backend_id?: string | null;
  client_id?: string | null;
  name?: string | null;
  public_base_url?: string | null;
  api_base_url?: string | null;
  ws_base_url?: string | null;
  frontend_base_url?: string | null;
  registered?: boolean;
}

export interface TokenResponse {
  access_token: string;
  refresh_token: string;
  token_type: string;
  user_id: string;
  username: string;
  role: string;
  handoff_code?: string;
  handoff_expires_at?: number;
  backend_connection?: BackendConnectionInfo | null;
  local_backend?: AuthzLocalBackendStatus | null;
}

export interface FileAttachment {
  file_id: string;
  name: string;
  content_type: string;
  size?: number;
  url?: string;
}

export interface ChatMessage {
  role: 'user' | 'assistant';
  content: string;
  timestamp?: string;
  attachments?: FileAttachment[];
  run_id?: string;
  task_id?: string | null;
  task_status?: string | null;
  evidence_status?: 'recorded';
  acceptance_state?: 'accept' | 'revise' | 'abandon';
  feedback_state?: 'accept' | 'satisfied' | 'revise' | 'abandon';
  feedback_error?: string;
  message_type?: string | null;
  scheduled_job_id?: string | null;
  scheduled_run_id?: string | null;
  cron_job_name?: string | null;
}

export interface Session {
  key: string;
  created_at?: string;
  updated_at?: string;
  path?: string;
}

export interface SessionDetail {
  key: string;
  messages: ChatMessage[];
  created_at: string;
  updated_at: string;
}

export interface ChatLogEvent {
  message_id?: number | null;
  run_id?: string | null;
  role: string;
  event_type?: string | null;
  content?: string | null;
  timestamp?: string;
  context_visible?: boolean;
  tool_name?: string | null;
  tool_call_id?: string | null;
  tool_calls?: Array<Record<string, unknown>> | null;
  finish_reason?: string | null;
  reasoning?: string | null;
  reasoning_details?: unknown;
  codex_reasoning_items?: unknown;
  event_payload?: Record<string, unknown> | null;
}

export interface ChatLogRun {
  run_id: string;
  session_id: string;
  title?: string | null;
  source?: string | null;
  task_id?: string | null;
  attempt_index?: number | null;
  task_mode?: boolean | null;
  user_input?: string;
  started_at?: string;
  ended_at?: string | null;
  finish_reason?: string | null;
  events: ChatLogEvent[];
}

export interface ChatLogSession {
  session_id: string;
  source?: string | null;
  title?: string | null;
  created_at?: string;
  updated_at?: string;
  runs: ChatLogRun[];
}

export interface ChatLogsResponse {
  sessions: ChatLogSession[];
}

export interface ProviderStatus {
  id?: string;
  name: string;
  label?: string;
  enabled?: boolean;
  active?: boolean;
  has_key: boolean;
  api_key_masked?: string;
  api_base?: string;
  default_api_base?: string;
  requires_api_key?: boolean;
  is_oauth?: boolean;
  is_local?: boolean;
  detail?: string;
}

export interface ProviderConfigPayload {
  enabled: boolean;
  model?: string;
  api_key?: string;
  api_base?: string;
  request_timeout_seconds?: number;
}

export interface AgentConfigPayload {
  max_tokens: number | null;
  temperature: number;
  max_tool_iterations: number;
}

export interface ChannelConfigDetail {
  channel_id: string;
  enabled: boolean;
  kind: string;
  mode: string;
  account_id: string;
  display_name: string;
  config: Record<string, unknown>;
  secrets: Record<string, string>;
}

export interface ChannelConfigPayload {
  enabled: boolean;
  kind: string;
  mode: string;
  account_id?: string;
  display_name?: string;
  config: Record<string, unknown>;
  secrets: Record<string, string>;
}

export interface ChannelStatus {
  channel_id: string;
  name?: string;
  kind: string;
  mode: string;
  display_name: string;
  enabled: boolean;
  state: 'configured' | 'disabled' | 'starting' | 'running' | 'degraded' | 'error' | 'stopped';
  account_id: string;
  last_error?: string | null;
  last_event_at?: string | null;
  started_at?: string | null;
  capabilities: string[];
  webhook_url?: string | null;
  websocket_url?: string | null;
  connected_peers?: number;
}

export interface ChannelEventRecord {
  event_id: string;
  channel_id: string;
  kind: string;
  session_id?: string | null;
  message_id?: string | null;
  run_id?: string | null;
  status: string;
  error?: string | null;
  text_preview?: string | null;
  text_length?: number;
  created_at: string;
  metadata?: Record<string, unknown>;
}

export interface ChannelConnectorDescriptor {
  kind: string;
  displayName?: string;
  display_name?: string;
  authType?: string;
  auth_type?: string;
  providerId?: string;
  provider_id?: string;
  capabilities?: string[];
}

export interface ChannelConnectionView {
  connection_id: string;
  owner_user_id?: string | null;
  channel_id: string;
  kind: string;
  mode: string;
  display_name: string;
  account_id: string;
  status: string;
  auth_type: string;
  runtime_config?: Record<string, unknown>;
  capabilities?: string[];
  created_at?: string;
  updated_at?: string;
  last_seen_at?: string | null;
  last_error?: string | null;
}

export interface ConnectorSessionView {
  sessionId: string;
  kind: string;
  status: string;
  qrCode?: string | null;
  qrImage?: string | null;
  instructions?: string[];
  accountId?: string | null;
  displayName?: string | null;
  error?: string | null;
  metadata?: Record<string, unknown>;
}

export interface ConnectorSessionResponse {
  session: ConnectorSessionView;
  connection?: ChannelConnectionView | null;
}

export interface ConnectorSessionStartPayload {
  kind: string;
  displayName?: string;
  ownerUserId?: string;
  options?: Record<string, unknown>;
}

export interface RuntimeControls {
  self_restart: boolean;
}

export interface SystemStatus {
  config_path: string;
  config_exists: boolean;
  workspace: string;
  workspace_exists: boolean;
  model: string;
  max_tokens: number | null;
  max_context_messages?: number;
  temperature: number;
  max_tool_iterations: number;
  providers: ProviderStatus[];
  channels: ChannelStatus[];
  runtime_controls?: RuntimeControls;
  cron: {
    enabled: boolean;
    jobs: number;
    next_wake_at_ms: number | null;
  };
  authz?: {
    enabled: boolean;
    base_url: string;
    outlook_mcp_url?: string;
    backend_id?: string;
    client_id?: string;
    registered: boolean;
  };
}

export interface Skill {
  name: string;
  description: string;
  source: 'builtin' | 'workspace';
  available: boolean;
  path: string;
  version?: string;
  status?: string;
  source_kind?: string;
  tool_hints?: string[];
  provenance?: Record<string, unknown>;
  agent_cards?: Record<string, unknown>[];
}

export interface SkillVersionRef {
  version: string;
  status?: string | null;
  createdAt?: string | null;
  publishedAt?: string | null;
  changeReason?: string | null;
  parentVersion?: string | null;
  contentHash?: string | null;
  fileCount?: number;
  totalSize?: number;
}

export interface SkillFileInfo {
  id?: number;
  filePath: string;
  fileSize: number;
  contentType?: string | null;
  sha256?: string | null;
}

export interface SkillFileContent extends SkillFileInfo {
  content?: string | null;
  isBinary?: boolean;
}

export interface SkillDetailResponse {
  skill: Skill;
  spec?: Record<string, unknown> | null;
  currentVersion: string;
  versions: SkillVersionRef[];
  versionDetail?: Record<string, unknown> | null;
  files: SkillFileInfo[];
  content: string;
  frontmatter?: Record<string, unknown>;
}

export interface CronJob {
  id: string;
  name: string;
  enabled: boolean;
  schedule_kind: 'at' | 'every' | 'cron';
  schedule_display: string;
  schedule_expr: string | null;
  schedule_every_ms: number | null;
  message: string;
  mode?: 'notification' | 'task';
  requires_followup?: boolean;
  deliver: boolean;
  channel: string | null;
  to: string | null;
  session_key?: string | null;
  next_run_at_ms: number | null;
  last_run_at_ms: number | null;
  last_status: string | null;
  last_error: string | null;
  last_scheduled_run_id?: string | null;
  last_task_id?: string | null;
  last_run_id?: string | null;
  history?: Array<{
    started_at_ms: number;
    finished_at_ms?: number | null;
    status: string;
    mode?: 'notification' | 'task';
    notification_session_id?: string | null;
    output?: string | null;
    task_id?: string | null;
    run_id?: string | null;
    error?: string | null;
    scheduled_run_id?: string;
    engaged?: boolean;
    engage_intent?: string | null;
  }>;
  created_at_ms: number;
  updated_at_ms?: number;
}

export interface NotificationRun {
  scheduled_run_id: string;
  job_id: string;
  job_name: string;
  title: string;
  message: string;
  status: string;
  mode: 'notification' | 'task';
  started_at_ms: number;
  finished_at_ms?: number | null;
  started_at?: string | null;
  finished_at?: string | null;
  output?: string | null;
  error?: string | null;
  notification_session_id: string;
  task_id?: string | null;
  run_id?: string | null;
  engaged?: boolean;
  engage_intent?: string | null;
}

export interface NotificationDetail extends NotificationRun {
  detail: SessionDetail;
}

export interface BackendTaskEvent {
  event_id: string;
  task_id: string;
  session_id: string;
  run_id?: string | null;
  event_type: string;
  created_at: string;
  payload: Record<string, unknown>;
}

export interface BackendTaskRunMessage {
  role: 'user' | 'assistant' | 'tool';
  content: string;
  created_at?: string;
  tool_name?: string | null;
}

export interface BackendTaskRun {
  run_id: string;
  title: string;
  session_id: string;
  started_at?: string | null;
  ended_at?: string | null;
  success?: boolean | null;
  finish_reason?: string | null;
  attempt_index?: number | null;
  task_text?: string;
  messages: BackendTaskRunMessage[];
  evidence_status?: string | null;
  validation_result?: Record<string, unknown> | null;
}

export interface BackendTask {
  task_id: string;
  session_id: string;
  parent_task_id?: string | null;
  description: string;
  short_title?: string | null;
  is_open?: boolean;
  goal: string;
  constraints: string[];
  priority: number;
  status: string;
  creator: string;
  created_at: string;
  updated_at: string;
  closed_at?: string | null;
  close_reason?: string | null;
  satisfaction?: number | null;
  run_ids: string[];
  skill_names: string[];
  feedback: Array<Record<string, unknown>>;
  validation_result?: Record<string, unknown> | null;
  metadata: Record<string, unknown>;
  events?: BackendTaskEvent[];
  runs?: BackendTaskRun[];
  process_runs?: ProcessRun[];
  process_events?: ProcessEvent[];
  process_artifacts?: ProcessArtifact[];
}

export interface ActiveTask {
  task_id: string;
  status: string;
  short_title: string;
  description: string;
  updated_at: string;
}

export interface SkillHubVersionRef {
  id?: number;
  version: string;
  status?: string;
  createdAt?: string;
  publishedAt?: string | null;
}

export interface SkillHubSearchItem {
  id?: number;
  slug: string;
  displayName: string;
  summary: string;
  namespace: string;
  downloadCount: number;
  starCount: number;
  ratingAvg?: number;
  ratingCount?: number;
  headlineVersion?: SkillHubVersionRef | null;
  publishedVersion?: SkillHubVersionRef | null;
  installed?: boolean;
  installed_version?: string | null;
  createdAt?: string;
  updatedAt?: string;
}

export interface SkillHubSearchResponse {
  items: SkillHubSearchItem[];
  total: number;
  page: number;
  size: number;
}

export interface SkillHubVersionsResponse {
  items: SkillVersionRef[];
  total: number;
  page: number;
  size: number;
}

export interface SkillHubFileInfo {
  id?: number;
  filePath: string;
  fileSize: number;
  contentType?: string | null;
  sha256?: string | null;
}

export interface SkillHubVersionDetail {
  id?: number;
  version?: string;
  status?: string;
  fileCount?: number;
  totalSize?: number;
  parsedMetadataJson?: string | null;
  manifestJson?: string | null;
  createdAt?: string;
  publishedAt?: string | null;
}

export interface SkillHubVersionResponse {
  detail: SkillHubVersionDetail;
  files: SkillHubFileInfo[];
}

export interface SkillHubInstallResponse {
  ok: boolean;
  skill_name: string;
  version: string;
  source: 'skillhub';
  namespace: string;
  slug: string;
  installed_path: string;
  already_installed?: boolean;
}

export type ProcessActorType = 'agent' | 'mcp' | 'system' | 'user';
export type ProcessRunStatus =
  | 'queued'
  | 'running'
  | 'waiting'
  | 'done'
  | 'error'
  | 'cancelled';
export type ProcessEventKind =
  | 'run_started'
  | 'run_progress'
  | 'run_message'
  | 'run_artifact'
  | 'run_status'
  | 'run_finished'
  | 'run_cancelled'
  | 'task_planned'
  | 'skill_selected'
  | 'tool_call_started'
  | 'tool_call_finished'
  | 'agent_team_created'
  | 'agent_finished'
  | 'agent_handoff'
  | 'task_result_ready'
  | 'task_acceptance_recorded'
  | 'task_error';

export interface UiAgentDescriptor {
  id: string;
  name: string;
  description: string;
  source: 'workspace' | 'plugin' | 'skill' | 'builtin';
  kind: string;
  protocol: string | null;
  endpoint?: string | null;
  base_url?: string | null;
  card_url?: string | null;
  auth_env?: string | null;
  auth_mode: string;
  auth_audience?: string | null;
  auth_scopes: string[];
  tags: string[];
  aliases: string[];
  metadata?: Record<string, unknown>;
  support_streaming: boolean;
}

export interface UiSubagentDescriptor {
  id: string;
  name: string;
  description: string;
  enabled: boolean;
  workspace: string;
  system_prompt: string;
  model?: string | null;
  delegation_mode: string;
  allow_mcp: boolean;
  tags: string[];
  aliases: string[];
  mcp_servers: Record<string, Record<string, unknown>>;
  metadata: Record<string, unknown>;
  base_url: string;
  endpoint: string;
  card_url: string;
}

export interface UiMcpServerDescriptor {
  id: string;
  name: string;
  transport: 'stdio' | 'http';
  kind?: 'local' | 'online';
  category?: string;
  managed?: boolean;
  source?: string;
  url?: string | null;
  command?: string | null;
  args?: string[];
  auth_mode: string;
  auth_audience?: string | null;
  auth_scopes: string[];
  headers?: Record<string, string>;
  env?: Record<string, string>;
  enabled: boolean;
  tool_timeout?: number;
  tool_count?: number;
  tool_names?: string[];
  status?: 'connected' | 'disconnected' | 'error';
  last_error?: string | null;
}

export interface AuthzLocalBackendStatus {
  backend_id?: string | null;
  client_id?: string | null;
  name?: string | null;
  public_base_url?: string | null;
  registered: boolean;
}

export interface AuthzStatus {
  enabled: boolean;
  base_url: string;
  outlook_mcp_url?: string;
  local_backend: AuthzLocalBackendStatus;
  backend?: Record<string, unknown>;
  permissions?: Record<string, unknown>;
  outlook?: Record<string, unknown>;
  channel_settings?: Record<string, unknown>;
  error?: string;
}

export interface OutlookMailboxAddress {
  emailAddress?: {
    name?: string | null;
    address?: string | null;
  } | null;
}

export interface OutlookFolderSummary {
  id: string | null;
  displayName: string | null;
  parentFolderId?: string | null;
  totalItemCount?: number | null;
  unreadItemCount?: number | null;
}

export interface OutlookMessageSummary {
  id: string | null;
  changekey?: string | null;
  subject: string | null;
  from?: OutlookMailboxAddress | null;
  receivedDateTime?: string | null;
  isRead?: boolean;
  bodyPreview?: string | null;
}

export interface OutlookMessageDetail extends OutlookMessageSummary {
  body?: {
    contentType: string;
    content: string;
  };
  toRecipients?: OutlookMailboxAddress[];
  ccRecipients?: OutlookMailboxAddress[];
}

export interface OutlookEventSummary {
  id: string | null;
  changekey?: string | null;
  subject: string | null;
  start?: {
    dateTime: string;
    timeZone: string;
  } | null;
  end?: {
    dateTime: string;
    timeZone: string;
  } | null;
  location?: {
    displayName?: string | null;
  } | null;
  bodyPreview?: string | null;
  attendees?: OutlookMailboxAddress[];
  organizer?: OutlookMailboxAddress | null;
}

export interface OutlookPageInfo {
  top: number;
  skip: number;
  returned: number;
  has_more: boolean;
  next_skip?: number | null;
}

export interface OutlookDefaultsFields {
  domain: string;
  service_endpoint: string;
  server: string;
  default_timezone: string;
  autodiscover: boolean;
}

export interface OutlookDefaults {
  provider: string;
  server_id: string;
  mcp_command: string;
  mcp_extra_args: string[];
  fields: OutlookDefaultsFields;
}

export interface OutlookSavedConnection {
  email: string;
  username?: string | null;
  domain?: string | null;
  service_endpoint?: string | null;
  server?: string | null;
  autodiscover: boolean;
  default_timezone: string;
}

export interface OutlookAuthStatus {
  provider: string;
  configured: boolean;
  authenticated: boolean;
  email?: string | null;
  server?: string | null;
  service_endpoint?: string | null;
  autodiscover?: boolean;
  username?: string | null;
}

export interface OutlookMeta {
  updated_at?: string;
  last_verified_at?: string;
  last_connected_at?: string;
  last_overview_refresh_at?: string;
  [key: string]: unknown;
}

export interface OutlookStatus {
  configured: boolean;
  connected: boolean;
  provider: string | null;
  storage_mode?: string;
  saved?: OutlookSavedConnection | null;
  auth_status?: OutlookAuthStatus | null;
  mcp_registered: boolean;
  mcp_server_id: string;
  defaults: OutlookDefaults;
  meta: OutlookMeta;
  error?: string | null;
}

export interface OutlookConnectionPayload {
  email: string;
  password: string;
  username?: string;
  domain?: string;
  service_endpoint?: string;
  server?: string;
  autodiscover: boolean;
  default_timezone: string;
}

export interface OutlookConnectionTestResult {
  ok: boolean;
  provider: string;
  mailbox: string;
  resolved_username: string;
  resolved_domain?: string | null;
  sample: {
    folders: OutlookFolderSummary[];
    inbox: OutlookMessageSummary[];
    events: OutlookEventSummary[];
  };
  warnings?: string[];
}

export interface OutlookConnectResult {
  ok: boolean;
  probe: OutlookConnectionTestResult['sample'];
  saved: {
    config_file: string;
    secrets_file: string;
    state_dir: string;
  };
  mcp: {
    id: string;
    command: string;
    args: string[];
    sensitive: boolean;
    tool_timeout: number;
  };
  meta: OutlookMeta;
}

export interface OutlookOverview {
  mailbox: string;
  timezone: string;
  today: string;
  connection: OutlookStatus;
  recentInbox: OutlookMessageSummary[];
  recentSent: OutlookMessageSummary[];
  todayEvents: OutlookEventSummary[];
  warnings?: string[];
  meta: OutlookMeta;
}

export interface OutlookMessageListResponse {
  folder: string;
  unread_only: boolean;
  value: OutlookMessageSummary[];
  page: OutlookPageInfo;
}

export interface OutlookEventListResponse {
  start_time: string;
  end_time: string;
  value: OutlookEventSummary[];
  page: OutlookPageInfo;
}

export interface ProcessRun {
  run_id: string;
  parent_run_id?: string | null;
  session_id?: string | null;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  title: string;
  source?: string | null;
  status: ProcessRunStatus;
  started_at: string;
  finished_at?: string | null;
  summary?: string | null;
  metadata?: Record<string, unknown>;
}

export interface ProcessEvent {
  event_id: string;
  run_id: string;
  parent_run_id?: string | null;
  kind: ProcessEventKind;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  text?: string;
  status?: ProcessRunStatus;
  message_role?: 'system' | 'user' | 'assistant' | 'tool';
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessArtifact {
  artifact_id: string;
  run_id: string;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name?: string;
  title: string;
  artifact_type: 'text' | 'json' | 'file' | 'image' | 'link' | 'markdown';
  content?: string;
  data?: Record<string, unknown> | unknown[];
  file_id?: string;
  url?: string;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export type TaskTimelineCardType =
  | 'task_created'
  | 'plan'
  | 'skill'
  | 'tool_call'
  | 'tool_result'
  | 'next_step'
  | 'agent_team'
  | 'agent_progress'
  | 'agent_handoff'
  | 'artifact'
  | 'error'
  | 'result'
  | 'result_history'
  | 'acceptance';

export interface TaskTimelineCard {
  id: string;
  taskId: string;
  runId?: string | null;
  parentRunId?: string | null;
  type: TaskTimelineCardType;
  title: string;
  summary?: string;
  actorName?: string;
  status?: string;
  createdAt: string;
  relatedArtifactIds?: string[];
  details?: Record<string, unknown>;
}

export interface SessionProcessProjection {
  runs: ProcessRun[];
  events: ProcessEvent[];
  artifacts: ProcessArtifact[];
  agents?: Array<Record<string, unknown>>;
}

export interface SkillLearningCandidate {
  candidate_id: string;
  kind: string;
  source_run_ids: string[];
  source_session_ids: string[];
  related_skill_names: string[];
  reason: string;
  evidence: Record<string, unknown>;
  status: string;
  priority?: number;
  confidence?: number;
  risk_level?: 'low' | 'medium' | 'high' | 'critical' | string;
  owner?: string | null;
  retry_count?: number;
  last_error?: string | null;
  trigger_reason?: string;
  evidence_summary?: string;
  draft_skill_name?: string | null;
  draft_id?: string | null;
  safety_report_id?: string | null;
  eval_report_id?: string | null;
  created_at?: string;
  updated_at?: string;
}

export interface SkillDraftSafetyReport {
  report_id: string;
  skill_name: string;
  draft_id: string;
  passed: boolean;
  risk_level: 'low' | 'medium' | 'high' | 'critical' | string;
  issues: string[];
  blocked_reasons: string[];
  suggested_fix: string;
  created_at: string;
}

export interface SkillDraftEvalReport {
  report_id: string;
  skill_name: string;
  draft_id: string;
  candidate_id: string;
  passed: boolean;
  baseline_score_avg: number;
  candidate_score_avg: number;
  score_delta: number;
  regression_count: number;
  improved_count: number;
  unchanged_count: number;
  cases: Array<Record<string, unknown>>;
  status: string;
  created_at: string;
  eval_version?: string;
  mode?: 'heuristic' | 'replay' | string;
  execution_coverage?: number;
  surrogate_coverage?: number;
  blocked_coverage?: number;
  confidence?: 'low' | 'medium' | 'high' | string;
  case_reports?: Array<Record<string, unknown>>;
  tool_mode_summary?: Record<string, unknown>;
  ability_score_summary?: Record<string, unknown>;
  tool_execution_summary?: Record<string, unknown>;
  case_selection_summary?: Record<string, unknown>;
  real_score_avg?: number | null;
  synthetic_score_avg?: number | null;
  overall_score_avg?: number | null;
  preservation_report?: Record<string, unknown> | null;
}

export interface SkillDraft {
  draft_id: string;
  skill_name: string;
  base_version?: string | null;
  target_version?: string | null;
  base_skill?: {
    skill_name: string;
    version: string;
    frontmatter: Record<string, unknown>;
    content: string;
    summary?: string;
    tool_hints?: string[];
  } | null;
  proposed_content: string;
  proposed_frontmatter: Record<string, unknown>;
  created_at: string;
  created_by: string;
  trigger_run_id?: string | null;
  trigger_session_id?: string | null;
  reason: string;
  status: string;
  evidence_refs: Array<Record<string, unknown>>;
  proposal_kind: string;
  reviews?: SkillReviewRecord[];
  safety_report?: SkillDraftSafetyReport | null;
  eval_report?: SkillDraftEvalReport | null;
}

export interface SkillReviewRecord {
  review_id: string;
  draft_id: string;
  skill_name: string;
  requested_at: string;
  requested_by: string;
  status: string;
  reviewer?: string | null;
  reviewed_at?: string | null;
  notes: string;
}

export interface ProcessRunStartedEvent {
  type: 'process_run_started';
  session_id?: string;
  run_id: string;
  parent_run_id?: string | null;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  source?: string | null;
  title: string;
  status: ProcessRunStatus;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessRunProgressEvent {
  type: 'process_run_progress';
  session_id?: string;
  run_id: string;
  parent_run_id?: string | null;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  text?: string;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessRunStatusEvent {
  type: 'process_run_status';
  session_id?: string;
  run_id: string;
  parent_run_id?: string | null;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  status: ProcessRunStatus;
  text?: string;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessRunMessageEvent {
  type: 'process_run_message';
  session_id?: string;
  run_id: string;
  parent_run_id?: string | null;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  message_role?: ProcessEvent['message_role'];
  text?: string;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessRunArtifactEvent {
  type: 'process_run_artifact';
  session_id?: string;
  run_id: string;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  title: string;
  artifact_type: ProcessArtifact['artifact_type'];
  content?: string;
  data?: Record<string, unknown> | unknown[];
  file_id?: string;
  url?: string;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessRunFinishedEvent {
  type: 'process_run_finished';
  session_id?: string;
  run_id: string;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  status: ProcessRunStatus;
  summary?: string | null;
  metadata?: Record<string, unknown>;
  created_at: string;
}

export interface ProcessRunCancelledEvent {
  type: 'process_run_cancelled';
  session_id?: string;
  run_id: string;
  actor_type: ProcessActorType;
  actor_id: string;
  actor_name: string;
  status: 'cancelled';
  created_at: string;
}

export interface ProcessCancelAckEvent {
  type: 'process_cancel_ack';
  run_id: string;
  ok: boolean;
}

export interface ChatAssistantEvent {
  type: 'message';
  role: 'assistant';
  content: string;
  attachments?: FileAttachment[];
  session_id?: string;
  run_id?: string;
  task_id?: string | null;
  task_status?: string | null;
  evidence_status?: 'recorded';
  validation_result?: Record<string, unknown> | null;
  metadata?: {
    task_id?: string | null;
    task_status?: string | null;
    evidence_status?: string | null;
    [key: string]: unknown;
  };
}

export interface ChatThinkingEvent {
  type: 'status';
  status: string;
}

export interface SessionUpdatedEvent {
  type: 'session_updated';
  session_id: string;
  source?: string;
  job_id?: string;
  job_name?: string;
}

export type ProcessWsEvent =
  | ProcessRunStartedEvent
  | ProcessRunProgressEvent
  | ProcessRunMessageEvent
  | ProcessRunStatusEvent
  | ProcessRunArtifactEvent
  | ProcessRunFinishedEvent
  | ProcessRunCancelledEvent
  | ProcessCancelAckEvent;

export type WsEvent = ChatAssistantEvent | ChatThinkingEvent | SessionUpdatedEvent | ProcessWsEvent;
