import client from "./client";
import type { CoverageSummary, TacticCoverage } from "../types/models";

// ── V1 — Coverage ───────────────────────────────────────────────────

/** Fetch the global coverage summary. */
export async function getCoverageSummary(): Promise<CoverageSummary> {
  const { data } = await client.get<CoverageSummary>("/metrics/summary");
  return data;
}

/** Fetch coverage breakdown by tactic. */
export async function getCoverageByTactic(): Promise<TacticCoverage[]> {
  const { data } = await client.get<TacticCoverage[]>("/metrics/by-tactic");
  return data;
}

// ── V2 — Test Pipeline ──────────────────────────────────────────────

export interface TestPipelineCounts {
  draft: number;
  red_executing: number;
  blue_evaluating: number;
  in_review: number;
  validated: number;
  rejected: number;
  total: number;
}

/** Fetch test counts per pipeline state. */
export async function getTestPipeline(): Promise<TestPipelineCounts> {
  const { data } = await client.get<TestPipelineCounts>("/metrics/test-pipeline");
  return data;
}

// ── V2 — Team Activity ──────────────────────────────────────────────

export interface TeamActivityItem {
  team: string;
  tests_completed: number;
  tests_pending: number;
  avg_completion_hours: number | null;
}

/** Fetch activity summary for Red and Blue teams. */
export async function getTeamActivity(): Promise<TeamActivityItem[]> {
  const { data } = await client.get<TeamActivityItem[]>("/metrics/team-activity");
  return data;
}

// ── V2 — Validation Rate ────────────────────────────────────────────

export interface ValidationRateItem {
  role: string;
  total_reviewed: number;
  approved: number;
  rejected: number;
  approval_rate: number;
}

/** Fetch approval/rejection rates for managers. */
export async function getValidationRate(): Promise<ValidationRateItem[]> {
  const { data } = await client.get<ValidationRateItem[]>("/metrics/validation-rate");
  return data;
}

// ── V2 — Recent Tests ───────────────────────────────────────────────

export interface RecentTestItem {
  id: string;
  name: string;
  state: string;
  technique_mitre_id: string | null;
  technique_name: string | null;
  created_at: string | null;
}

/** Fetch the 10 most recently updated tests. */
export async function getRecentTests(): Promise<RecentTestItem[]> {
  const { data } = await client.get<RecentTestItem[]>("/metrics/recent-tests");
  return data;
}
