import client from "./client";

// ── Types ───────────────────────────────────────────────────────────

export interface SnapshotSummary {
  id: string;
  name: string | null;
  organization_score: number;
  total_techniques: number;
  validated_count: number;
  partial_count: number;
  not_covered_count: number;
  in_progress_count: number;
  not_evaluated_count: number;
  coverage_percentage?: number;
  by_tactic?: Record<string, unknown>;
  by_status?: Record<string, number>;
  stale_count?: number;
  never_tested_count?: number;
  created_by: string | null;
  created_at: string | null;
}

export interface CoverageEvolutionPoint {
  date: string | null;
  name: string | null;
  org_score: number;
  coverage_pct: number;
  by_tactic: Record<string, unknown>;
  by_status: Record<string, number>;
  stale_count: number;
  never_tested_count: number;
  validated_count: number;
  total_techniques: number;
}

export interface TechniqueState {
  mitre_id: string;
  technique_id: string;
  status: string;
  score: number | null;
}

export interface SnapshotDetail extends SnapshotSummary {
  technique_states: TechniqueState[];
}

export interface SnapshotComparisonDelta {
  mitre_id: string;
  old_status: string;
  new_status: string;
  old_score: number;
  new_score: number;
}

export interface SnapshotComparison {
  snapshot_a: SnapshotSummary;
  snapshot_b: SnapshotSummary;
  score_delta: number;
  improved: SnapshotComparisonDelta[];
  worsened: SnapshotComparisonDelta[];
  unchanged_count: number;
  summary: {
    improved_count: number;
    worsened_count: number;
    new_count: number;
  };
}

// ── API Functions ───────────────────────────────────────────────────

/** List snapshots (paginated, newest first). */
export async function listSnapshots(params?: {
  offset?: number;
  limit?: number;
}): Promise<{ total: number; items: SnapshotSummary[] }> {
  const { data } = await client.get("/snapshots", { params });
  return data;
}

/** Create a manual snapshot. */
export async function createSnapshot(name?: string): Promise<SnapshotSummary> {
  const { data } = await client.post<SnapshotSummary>("/snapshots", {
    name: name || null,
  });
  return data;
}

/** Get snapshot detail with per-technique states. */
export async function getSnapshot(snapshotId: string): Promise<SnapshotDetail> {
  const { data } = await client.get<SnapshotDetail>(
    `/snapshots/${snapshotId}`,
  );
  return data;
}

/** Coverage trend points for dashboard charts. */
export async function getCoverageEvolution(
  months = 12,
): Promise<CoverageEvolutionPoint[]> {
  const { data } = await client.get<CoverageEvolutionPoint[]>(
    "/snapshots/evolution",
    { params: { months } },
  );
  return data;
}

/** Compare two snapshots. */
export async function compareSnapshots(
  aId: string,
  bId: string,
): Promise<SnapshotComparison> {
  const { data } = await client.get<SnapshotComparison>("/snapshots/compare", {
    params: { a: aId, b: bId },
  });
  return data;
}

/** Delete a snapshot (admin only). */
export async function deleteSnapshot(snapshotId: string): Promise<void> {
  await client.delete(`/snapshots/${snapshotId}`);
}
