/** Arena types — single source of truth for cross-app Arena wire contracts. */

import { z } from 'zod';

export type BattleType = 'coding' | 'qa';
export type BattleStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
export type ContestantStatus = 'queued' | 'running' | 'done' | 'error';
export type ContestantLane = 'local' | 'cloud';

export const TokenBreakdownSchema = z.object({
  system: z.number().int().nonnegative(),
  user: z.number().int().nonnegative(),
  assistant: z.number().int().nonnegative(),
  tools: z.number().int().nonnegative(),
  reasoning: z.number().int().nonnegative(),
  total: z.number().int().nonnegative(),
});

export type TokenBreakdown = z.infer<typeof TokenBreakdownSchema>;

// Pane state — carried on the WorkspacePane row, mirrors OrchestratorState.
export interface ArenaState {
  battle_id: string;
  battle_type: BattleType;
  prompt: string;
}

export interface BattleShape {
  id: string;
  project_id: string;
  battle_type: BattleType;
  prompt: string;
  status: BattleStatus;
  winner_contestant_id: string | null;
  results_path: string | null;
  error: string | null;
  created_at: string;
  updated_at: string;
}

export interface ContestantShape {
  id: string;
  battle_id: string;
  /** Backend name (coding) or persona name (qa). Unique per (battle, model) pair. */
  identity: string;
  model: string;
  lane: ContestantLane;
  task_id: string | null;
  worktree_id: string | null;
  status: ContestantStatus;
  duration_ms: number | null;
  tokens_per_sec: number | null;
  cost_tokens: number | null;
  token_breakdown: TokenBreakdown | null;
  result_path: string | null;
  error: string | null;
  created_at: string;
  updated_at: string;
}

export interface CrossExaminationShape {
  id: string;
  battle_id: string;
  /** Backend + model performing the cross-examination. */
  identity: string;
  model: string;
  verdict: string | null;
  created_at: string;
}
