/**
 * @agentguard/sdk — Request and response types.
 *
 * These types mirror the Pydantic schemas in agentguard/server/schemas.py.
 * Any language SDK should implement these exact same types.
 */

// ------------------------------------------------------------------ //
//  Shared
// ------------------------------------------------------------------ //

export interface TraceSummary {
  id: string;
  total_llm_calls: number;
  total_cost: { total: string; currency: string };
  structural_fixes: number;
  challenge_reworks: number;
  duration_ms: number;
  levels_completed: string[];
}

export interface CheckResult {
  check: string;
  passed: boolean;
  details: string;
  duration_ms: number;
}

export interface AutoFix {
  file: string;
  fix: string;
  line: number | null;
}

export interface ValidationError {
  check: string;
  file_path: string;
  line: number | null;
  column: number | null;
  message: string;
  severity: string;
  code: string | null;
}

export interface CriterionResult {
  criterion: string;
  passed: boolean;
  explanation: string;
}

// ------------------------------------------------------------------ //
//  /v1/generate
// ------------------------------------------------------------------ //

export interface GenerateOptions {
  skip_challenge?: boolean;
  skip_validation?: boolean;
  parallel_l4?: boolean;
  max_challenge_retries?: number;
}

export interface GenerateRequest {
  spec: string;
  archetype?: string;
  llm?: string;
  options?: GenerateOptions;
}

export interface GenerateResult {
  files: Record<string, string>;
  trace: TraceSummary;
}

// ------------------------------------------------------------------ //
//  /v1/validate
// ------------------------------------------------------------------ //

export interface ValidateRequest {
  files: Record<string, string>;
  archetype?: string;
  checks?: string[];
}

export interface ValidationReport {
  passed: boolean;
  checks: CheckResult[];
  auto_fixed: AutoFix[];
  errors: ValidationError[];
}

// ------------------------------------------------------------------ //
//  /v1/challenge
// ------------------------------------------------------------------ //

export interface ChallengeRequest {
  code: string;
  criteria?: string[];
  llm?: string;
}

export interface ChallengeResult {
  passed: boolean;
  attempt: number;
  criteria_results: CriterionResult[];
  assumptions: string[];
  grounding_violations: string[];
  feedback: string | null;
  cost: { total: string; currency: string };
}

// ------------------------------------------------------------------ //
//  /v1/archetypes
// ------------------------------------------------------------------ //

export interface ArchetypeSummary {
  id: string;
  name: string;
  description: string;
}

export interface ArchetypeDetail {
  id: string;
  name: string;
  description: string;
  version: string;
  tech_stack: Record<string, string>;
  pipeline: Record<string, unknown>;
  structure: Record<string, string[]>;
  validation: Record<string, unknown>;
  self_challenge: Record<string, unknown>;
  reference_patterns: string[];
}

// ------------------------------------------------------------------ //
//  /v1/traces
// ------------------------------------------------------------------ //

export interface TraceListItem {
  id: string;
  archetype: string;
  spec_summary: string;
  started_at: string;
  total_cost: string;
  duration_ms: number;
}

export interface TraceDetail {
  id: string;
  archetype: string;
  spec_summary: string;
  started_at: string;
  ended_at: string | null;
  spans: Record<string, unknown>[];
  summary: TraceSummary;
}

// ------------------------------------------------------------------ //
//  SSE streaming events
// ------------------------------------------------------------------ //

export interface SSELevelEvent {
  event: "level_complete";
  level: string;
  files: string[];
  duration_ms: number;
  cost: string;
}

export interface SSEValidationEvent {
  event: "validation";
  passed: boolean;
  errors: number;
  fixes: number;
}

export interface SSEChallengeEvent {
  event: "challenge";
  passed: boolean;
  criteria_passed: number;
  criteria_total: number;
  rework: boolean;
}

export interface SSECompleteEvent {
  event: "complete";
  files: Record<string, string>;
  trace: TraceSummary;
}

export interface SSEErrorEvent {
  event: "error";
  error: string;
}

export type SSEEvent =
  | SSELevelEvent
  | SSEValidationEvent
  | SSEChallengeEvent
  | SSECompleteEvent
  | SSEErrorEvent;

// ------------------------------------------------------------------ //
//  Health
// ------------------------------------------------------------------ //

export interface HealthResponse {
  status: string;
  version: string;
}

// ------------------------------------------------------------------ //
//  Error (RFC 7807)
// ------------------------------------------------------------------ //

export interface ProblemDetail {
  type: string;
  title: string;
  status: number;
  detail: string;
  instance: string;
}

// ------------------------------------------------------------------ //
//  Client config
// ------------------------------------------------------------------ //

export interface AgentGuardConfig {
  /** Base URL of the AgentGuard server. Default: http://localhost:8420 */
  url?: string;
  /** API key for X-Api-Key header. Optional for local mode. */
  apiKey?: string;
  /** Request timeout in ms. Default: 300_000 (5 minutes). */
  timeout?: number;
  /** Custom fetch implementation (for testing or Node 16). */
  fetch?: typeof globalThis.fetch;
}
