import client from "./client";

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

export interface ThreatActorSummary {
  id: string;
  mitre_id: string | null;
  name: string;
  aliases: string[];
  country: string | null;
  target_sectors: string[];
  target_regions: string[];
  motivation: string | null;
  sophistication: string | null;
  mitre_url: string | null;
  technique_count: number;
  coverage_pct: number;
  is_active: boolean;
}

export interface ThreatActorListResponse {
  total: number;
  offset: number;
  limit: number;
  items: ThreatActorSummary[];
}

export interface ThreatActorTechnique {
  technique_id: string;
  mitre_id: string;
  name: string;
  tactic: string | null;
  status_global: string | null;
  usage_description: string | null;
  first_seen_using: string | null;
}

export interface ThreatActorDetail {
  id: string;
  mitre_id: string | null;
  name: string;
  aliases: string[];
  description: string | null;
  country: string | null;
  target_sectors: string[];
  target_regions: string[];
  motivation: string | null;
  sophistication: string | null;
  first_seen: string | null;
  last_seen: string | null;
  references: Array<{ source: string; url: string; description: string }>;
  mitre_url: string | null;
  is_active: boolean;
  techniques: ThreatActorTechnique[];
}

export interface CoverageResponse {
  actor_id: string;
  actor_name: string;
  total_techniques: number;
  covered: number;
  coverage_pct: number;
  breakdown: Record<string, number>;
}

export interface GapItem {
  technique_id: string;
  mitre_id: string;
  name: string;
  tactic: string | null;
  status_global: string | null;
  usage_description: string | null;
  available_templates: number;
  existing_tests: number;
  has_templates: boolean;
}

export interface GapsResponse {
  actor_id: string;
  actor_name: string;
  total_gaps: number;
  gaps: GapItem[];
}

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

export interface ListThreatActorsParams {
  search?: string;
  country?: string;
  motivation?: string;
  sophistication?: string;
  target_sectors?: string;
  offset?: number;
  limit?: number;
}

/** List threat actors with filters. */
export async function getThreatActors(
  params?: ListThreatActorsParams
): Promise<ThreatActorListResponse> {
  const { data } = await client.get<ThreatActorListResponse>("/threat-actors", {
    params,
  });
  return data;
}

/** Get detailed info about a threat actor. */
export async function getThreatActor(id: string): Promise<ThreatActorDetail> {
  const { data } = await client.get<ThreatActorDetail>(`/threat-actors/${id}`);
  return data;
}

/** Get coverage analysis for a threat actor. */
export async function getThreatActorCoverage(id: string): Promise<CoverageResponse> {
  const { data } = await client.get<CoverageResponse>(`/threat-actors/${id}/coverage`);
  return data;
}

/** Get gap analysis for a threat actor. */
export async function getThreatActorGaps(id: string): Promise<GapsResponse> {
  const { data } = await client.get<GapsResponse>(`/threat-actors/${id}/gaps`);
  return data;
}
