// ═══════════════════════════════════════════════════════════
// Task Types — from CONTRACTS.md §1.4
// ═══════════════════════════════════════════════════════════

import type { PipelineStage, Priority } from "./pipeline.js";

export type TaskType = "approval" | "review" | "decision" | "manual_action" | "fix_required";
export type TaskStatus = "pending" | "claimed" | "in_progress" | "completed" | "expired" | "escalated";
export type TaskDecision = "approved" | "rejected" | "deferred" | "escalated";

export interface Task {
  id: string;
  pipelineId: string | null;
  stageName: PipelineStage | null;
  type: TaskType;
  title: string;
  description: string | null;
  context: TaskContext;
  status: TaskStatus;
  priority: Priority;
  assigneeId: string | null;
  claimedAt: string | null;
  claimedBy: string | null;
  decision: TaskDecision | null;
  decisionNotes: string | null;
  decisionData: Record<string, unknown>;
  decidedAt: string | null;
  decidedBy: string | null;
  slaDeadline: string | null;
  slaWarningsSent: number;
  slaBreached: boolean;
  escalationLevel: number;
  blocksStageAdvance: boolean;
  blocksPipelineId: string | null;
  createdAt: string;
  updatedAt: string;
}

export interface TaskContext {
  summary: string;
  details?: string;
  testResults?: TestResults;
  diffSummary?: DiffSummary;
  metrics?: Record<string, number>;
  relatedAssets?: string[];
  modalType?: string;
  modalConfig?: Record<string, unknown>;
}

export interface TestResults {
  total: number;
  passed: number;
  failed: number;
  skipped: number;
  coveragePercent: number;
  failureDetails?: TestFailure[];
}

export interface TestFailure {
  testName: string;
  error: string;
  file?: string;
  line?: number;
}

export interface DiffSummary {
  filesChanged: number;
  insertions: number;
  deletions: number;
  files: DiffFile[];
}

export interface DiffFile {
  path: string;
  status: "added" | "modified" | "deleted" | "renamed";
  insertions: number;
  deletions: number;
  patch?: string;
}

export interface TaskStats {
  pending: number;
  inProgress: number;
  blocked: number;
  avgWaitTimeMinutes: number;
  slaBreaches: number;
  byPriority: Record<Priority, number>;
  byType: Record<TaskType, number>;
}

export interface BlockerInfo {
  blockerType: "task" | "stage" | "agent" | "dependency";
  entityId: string;
  title: string;
  pipelineId: string;
  pipelineName: string;
  priority: Priority;
  hoursBlocked: number;
  blockReason: string;
  suggestedAction?: string;
}
