import client from "./client";

export interface DefensiveTechnique {
  id: string;
  d3fend_id: string;
  name: string;
  description: string | null;
  tactic: string | null;
  d3fend_url: string | null;
}

export interface DefensesForTechnique {
  mitre_id: string;
  technique_name: string;
  defenses: DefensiveTechnique[];
  total: number;
}

export interface D3FENDTactic {
  tactic: string;
  count: number;
}

export interface D3FENDImportResult {
  techniques: { created: number; updated: number; total: number };
  mappings: { created: number; skipped: number; total: number };
}

/** Fetch defenses for a specific ATT&CK technique. */
export async function getDefensesForTechnique(mitreId: string): Promise<DefensesForTechnique> {
  const { data } = await client.get<DefensesForTechnique>(`/d3fend/for-technique/${mitreId}`);
  return data;
}

/** List all defensive techniques with optional filters. */
export async function listDefensiveTechniques(params?: {
  tactic?: string;
  search?: string;
  offset?: number;
  limit?: number;
}): Promise<{ total: number; items: DefensiveTechnique[] }> {
  const { data } = await client.get("/d3fend", { params });
  return data;
}

/** Get D3FEND tactic counts. */
export async function getD3FENDTactics(): Promise<D3FENDTactic[]> {
  const { data } = await client.get<D3FENDTactic[]>("/d3fend/tactics");
  return data;
}

/** Trigger D3FEND import (admin only). */
export async function triggerD3FENDImport(): Promise<D3FENDImportResult> {
  const { data } = await client.post<D3FENDImportResult>("/d3fend/import");
  return data;
}
