/**
 * Shape of a single row returned by `GET /tasks`. Kept minimal — only the
 * fields the dashboard kanban actually renders. Extend as consumers grow.
 */
export interface Task {
  id: string;
  task_id?: string | null;
  submission_id?: string | null;
  task_kind: string;
  status: string;
  execution_path?: string | null;
  workflow_id?: string | null;
  turn: number;
  pr_url: string | null;
  error: string | null;
  source: string | null;
  parent_id: string | null;
  external_id: string | null;
  repo: string | null;
  description: string | null;
  created_at: string | null;
  updated_at?: string | null;
  phase: string | null;
  depends_on: string[];
  subtask_ids: string[];
  project: string | null;
  workflow?: WorkflowSummary | null;
  scheduler: TaskSchedulerState;
}

export interface TaskSchedulerOwner {
  kind: string;
  id: string;
}

export interface TaskSchedulerState {
  authority_state: string;
  owner?: TaskSchedulerOwner | null;
  run_generation: number;
  recovery_generation: number;
  lease_expires_at?: string | null;
}

export interface TaskListPage {
  limit: number;
  has_more: boolean;
  next_cursor?: string | null;
}

export interface TaskListCounts {
  total: number;
  running: number;
  failed: number;
  by_status: Record<string, number>;
  by_scheduler_state: Record<string, number>;
}

export interface TaskListResponse {
  data: Task[];
  page: TaskListPage;
  counts: TaskListCounts;
}

export interface TaskArtifact {
  task_id: string;
  turn: number;
  artifact_type: string;
  content: string;
  created_at: string;
}

export interface TaskPrompt {
  task_id: string;
  turn: number;
  phase: string;
  prompt: string;
  created_at: string;
}

export function isTerminal(status: string): boolean {
  return status === "done" || status === "cancelled" || status === "failed";
}

export interface WorkflowSummary {
  id?: string | null;
  definition_id?: string | null;
  state: string;
  project_id?: string | null;
  issue_number?: number | null;
  pr_number?: number | null;
  force_execute?: boolean;
  plan_concern?: string | null;
  review_fallback?: ReviewFallbackSummary | null;
}

export interface ReviewFallbackSummary {
  tier: string;
  trigger: string;
  active_bot?: string | null;
  activated_at: string;
}

export interface RoundItem {
  kind: string;
  content?: string | null;
}

export interface TaskRound {
  items: RoundItem[];
}

export interface FullTask extends Task {
  rounds: TaskRound[];
  system_input?: string | null;
  request_settings?: string | null;
}

export type StreamItem =
  | { type: "MessageDelta"; text: string }
  | { type: "Done" }
  | { type: "Error"; message: string }
  | { type: "TokenUsage"; usage: { input_tokens: number; output_tokens: number } };

export type CreateTaskPayload =
  | { issue: number; project?: string; agent?: string; max_turns?: number; turn_timeout_secs?: number }
  | { pr: number; project?: string; agent?: string; max_turns?: number; turn_timeout_secs?: number }
  | { prompt: string; project?: string; agent?: string; max_turns?: number; turn_timeout_secs?: number };

export interface CreateTaskResponse {
  task_id: string;
  submission_id?: string | null;
  status: string;
  execution_path?: string;
  workflow_id?: string | null;
}
