import client from '../client';

/* ── Types ── */

export interface Report {
  id: string;
  tenancy_name: string;
  status: string;
  level: number | null;
  regions?: string[];
  obp?: boolean;
  raw?: boolean;
  redact_output?: boolean;
  created_at: string;
  completed_at: string | null;
  error_msg?: string | null;
}

export interface RunReportBody {
  config_id: string;
  regions?: string[];
  level: 1 | 2;
  obp: boolean;
  raw: boolean;
  redact_output: boolean;
}

export interface RunReportResult {
  report_id: string;
  status: string;
}

export interface ReportProgress {
  status: string;
  progress: string[];
  created_at: string;
  completed_at: string | null;
  error_msg: string | null;
}

export interface ReportSummary {
  tenancy: string;
  level: number;
  regions: string[];
  generated_at: string;
  total: number;
  passed: number;
  failed: number;
  total_findings: number;
  total_compliant: number;
  score: number;
  sections: Record<string, { passed: number; failed: number; total: number }>;
}

export interface ReportFile {
  id: string;
  file_name: string;
  file_type: string;
  file_category: string;
  file_size: number;
}

export interface CisEngineVersion {
  version: string;
  updated_date: string;
  file_path: string;
}

export interface CisEngineUpdate {
  local_version: string;
  local_date: string;
  remote_version: string;
  remote_date: string;
  update_available: boolean;
}

/* ── API ── */

export const reportsApi = {
  run: (body: RunReportBody) =>
    client.post('/reports/run', body) as unknown as Promise<RunReportResult>,

  list: () =>
    client.get('/reports') as unknown as Promise<Report[]>,

  progress: (rid: string) =>
    client.get(`/reports/${rid}/progress`) as unknown as Promise<ReportProgress>,

  cancel: (rid: string) =>
    client.post(`/reports/${rid}/cancel`) as unknown as Promise<{ ok: boolean }>,

  delete: (rid: string) =>
    client.delete(`/reports/${rid}`) as unknown as Promise<{ ok: boolean }>,

  summary: (rid: string) =>
    client.get(`/reports/${rid}/summary`) as unknown as Promise<ReportSummary>,

  htmlUrl: (rid: string) => {
    const token = localStorage.getItem('t') || '';
    return `/api/reports/${rid}/html?token=${encodeURIComponent(token)}`;
  },

  complianceReportUrl: (rid: string) => {
    const token = localStorage.getItem('t') || '';
    return `/api/reports/${rid}/compliance-report?token=${encodeURIComponent(token)}`;
  },

  generateComplianceReport: (rid: string) =>
    client.post(`/reports/${rid}/compliance-report/generate`) as unknown as Promise<{ status: string; has_rag: boolean }>,

  complianceReportStatus: (rid: string) =>
    client.get(`/reports/${rid}/compliance-report/status`) as unknown as Promise<{ ready: boolean }>,

  files: (rid: string) =>
    client.get(`/reports/${rid}/files`) as unknown as Promise<ReportFile[]>,

  downloadUrl: (rid: string, fid: string) => {
    const token = localStorage.getItem('t') || '';
    return `/api/reports/${rid}/files/${fid}/download?token=${encodeURIComponent(token)}`;
  },

  engineVersion: () =>
    client.get('/cis-engine/version') as unknown as Promise<CisEngineVersion>,

  engineCheckUpdate: () =>
    client.get('/cis-engine/check-update') as unknown as Promise<CisEngineUpdate>,

  engineUpdate: () =>
    client.post('/cis-engine/update') as unknown as Promise<{ ok: boolean }>,

  embedReport: (rid: string, adbConfigId: string, tableName?: string) =>
    client.post(`/embeddings/report/${rid}`, {
      adb_config_id: adbConfigId,
      ...(tableName ? { table_name: tableName } : {}),
    }) as unknown as Promise<{ ok: boolean; message: string; sections: number; tenancy: string }>,

  embedFile: (rid: string, fid: string, adbConfigId: string, tableName?: string) =>
    client.post(`/embeddings/report/${rid}/file/${fid}`, {
      adb_config_id: adbConfigId,
      ...(tableName ? { table_name: tableName } : {}),
    }) as unknown as Promise<{ ok: boolean; message: string; chunks: number }>,
};
