import client from "./client";
import type { AttackSuccessResult } from "../types/models";

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

export interface CoverageReportSummary {
  total_techniques: number;
  validated: number;
  partial: number;
  not_covered: number;
  in_progress: number;
  not_evaluated: number;
  coverage_percentage: number;
}

export interface CoverageTechniqueRow {
  mitre_id: string;
  name: string;
  tactic: string | null;
  platforms: string[];
  status_global: string;
  total_tests: number;
  tests_by_state: Record<string, number>;
}

export interface CoverageReport {
  generated_at: string;
  summary: CoverageReportSummary;
  techniques: CoverageTechniqueRow[];
}

export interface TestResultsReport {
  generated_at: string;
  filters: Record<string, string | null>;
  summary: {
    total_tests: number;
    by_state: Record<string, number>;
    by_detection_result: Record<string, number>;
  };
  tests: Array<{
    id: string;
    name: string;
    technique_id: string;
    state: string;
    platform: string | null;
    attack_success: AttackSuccessResult | null;
    detection_result: string | null;
    red_validation_status: string | null;
    blue_validation_status: string | null;
    created_at: string | null;
  }>;
}

export interface RemediationReport {
  generated_at: string;
  summary: {
    total_with_remediation: number;
    by_status: Record<string, number>;
  };
  tests: Array<{
    id: string;
    name: string;
    technique_id: string;
    state: string;
    remediation_status: string | null;
    remediation_steps: string | null;
    remediation_assignee: string | null;
  }>;
}

export interface ReportFilters {
  tactic?: string;
  platform?: string;
  state?: string;
  date_from?: string;
  date_to?: string;
  status?: string;
}

// ── API ─────────────────────────────────────────────────────────────

export async function getCoverageSummary(
  filters?: ReportFilters,
): Promise<CoverageReport> {
  const params = new URLSearchParams();
  if (filters?.tactic) params.set("tactic", filters.tactic);
  if (filters?.platform) params.set("platform", filters.platform);
  const { data } = await client.get<CoverageReport>(
    `/reports/coverage-summary?${params.toString()}`,
  );
  return data;
}

export function getCoverageCsvUrl(filters?: ReportFilters): string {
  const params = new URLSearchParams();
  if (filters?.tactic) params.set("tactic", filters.tactic);
  if (filters?.platform) params.set("platform", filters.platform);
  return `/api/v1/reports/coverage-csv?${params.toString()}`;
}

export async function getTestResults(
  filters?: ReportFilters,
): Promise<TestResultsReport> {
  const params = new URLSearchParams();
  if (filters?.state) params.set("state", filters.state);
  if (filters?.date_from) params.set("date_from", filters.date_from);
  if (filters?.date_to) params.set("date_to", filters.date_to);
  const { data } = await client.get<TestResultsReport>(
    `/reports/test-results?${params.toString()}`,
  );
  return data;
}

export async function getRemediationStatus(
  filters?: ReportFilters,
): Promise<RemediationReport> {
  const params = new URLSearchParams();
  if (filters?.status) params.set("status", filters.status);
  const { data } = await client.get<RemediationReport>(
    `/reports/remediation-status?${params.toString()}`,
  );
  return data;
}
