import client from "./client";

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

export interface ScoreBreakdownItem {
  score: number;
  max: number;
  detail: string;
}

export interface TechniqueScore {
  mitre_id: string;
  name: string;
  tactic: string | null;
  status_global: string | null;
  total_score: number;
  breakdown: Record<string, ScoreBreakdownItem>;
}

export interface TacticScore {
  tactic: string;
  average_score: number;
  techniques_count: number;
  techniques_scored: number;
}

export interface ActorCoverageScore {
  actor_id: string;
  actor_name: string;
  total_score: number;
  techniques_count: number;
  techniques_covered: number;
  techniques_detail: Array<{
    mitre_id: string;
    name: string;
    score: number;
    breakdown: Record<string, ScoreBreakdownItem>;
  }>;
}

export interface OrganizationScore {
  overall_score: number;
  total_coverage: number;
  critical_coverage: number;
  detection_maturity: number;
  response_readiness: number;
  techniques_evaluated: number;
  techniques_total: number;
}

export interface ScoreHistoryPoint {
  date: string;
  score: number;
  validated_tests: number;
}

export interface ScoringConfig {
  weights: {
    tests: number;
    detection_rules: number;
    d3fend: number;
    freshness: number;
    platform_diversity: number;
  };
  total: number;
}

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

export async function getTechniqueScore(mitreId: string): Promise<TechniqueScore> {
  const { data } = await client.get<TechniqueScore>(`/scores/technique/${mitreId}`);
  return data;
}

export async function getTacticScore(tactic: string): Promise<TacticScore> {
  const { data } = await client.get<TacticScore>(`/scores/tactic/${tactic}`);
  return data;
}

export async function getActorCoverageScore(actorId: string): Promise<ActorCoverageScore> {
  const { data } = await client.get<ActorCoverageScore>(`/scores/threat-actor/${actorId}`);
  return data;
}

export async function getOrganizationScore(): Promise<OrganizationScore> {
  const { data } = await client.get<OrganizationScore>("/scores/organization");
  return data;
}

export async function getScoreHistory(period: string = "90d"): Promise<ScoreHistoryPoint[]> {
  const { data } = await client.get<ScoreHistoryPoint[]>("/scores/history", {
    params: { period },
  });
  return data;
}

export async function getScoringConfig(): Promise<ScoringConfig> {
  const { data } = await client.get<ScoringConfig>("/scores/config");
  return data;
}
