/**
 * Task state management types
 */

export type TaskStatus = "pending" | "assigned" | "executing" | "completed" | "failed";

export interface TaskContext {
  repository: string;
  branch: string;
  workItems: string[];
  skills?: string[];
}

export interface TaskState {
  taskId: string;
  status: TaskStatus;
  agentId?: string;
  context: TaskContext;
  createdAt: string;
  updatedAt: string;
  metadata?: Record<string, unknown>;
}

/**
 * Agent state management types
 */

export type AgentStatus = "spawning" | "running" | "completed" | "failed" | "killed";

export interface AgentState {
  agentId: string;
  status: AgentStatus;
  taskId: string;
  startedAt?: string;
  completedAt?: string;
  error?: string;
  metadata?: Record<string, unknown>;
}

/**
 * State transition validation
 */

export const VALID_TASK_TRANSITIONS: Record<TaskStatus, TaskStatus[]> = {
  pending: ["assigned", "failed"],
  assigned: ["executing", "failed"],
  executing: ["completed", "failed"],
  completed: [],
  failed: ["pending"], // Allow retry
};

export const VALID_AGENT_TRANSITIONS: Record<AgentStatus, AgentStatus[]> = {
  spawning: ["running", "failed", "killed"],
  running: ["completed", "failed", "killed"],
  completed: [],
  failed: [],
  killed: [],
};

/**
 * Validate state transition
 */
export function isValidTaskTransition(from: TaskStatus, to: TaskStatus): boolean {
  return VALID_TASK_TRANSITIONS[from].includes(to);
}

export function isValidAgentTransition(from: AgentStatus, to: AgentStatus): boolean {
  return VALID_AGENT_TRANSITIONS[from].includes(to);
}
