import { get, post, proj, upload } from "./http";
import type { Page, ReportType, StageKey } from "./types";
import type { OutcomeLine, OutcomeReport, ReportDraftContext } from "./types-extra";

/** Outcome reports — master plan G.8, §20. Every listed trainee gets Yes or No (non-completers are explicit). */
export const getReportContext = (classId: string, type: ReportType, stageKey?: StageKey) =>
  get<ReportDraftContext>(proj(`/classes/${classId}/outcomes/${type}/context`), {
    stage: stageKey,
  });
export const outcomeTemplateUrl = (
  classId: string,
  type: ReportType,
  stageKey?: StageKey,
  format: "xlsx" | "csv" = "xlsx",
) =>
  `${proj(`/classes/${classId}/outcomes/${type}/template`)}?format=${format}${stageKey ? `&stage=${stageKey}` : ""}`;
/** Per-provider list of trainees reported "Yes" for one report type (V1 "Export report"). */
export const OUTCOME_EXPORTS: [string, string][] = [
  ["completion", "Completers"],
  ["certification", "Certified"],
  ["dropout", "Dropouts"],
  ["employment_placement", "Employed (placement)"],
  ["locality_compliant", "Locality compliant"],
  ["job_retention_3m", "Retained 3 months"],
];
export const outcomeExportUrl = (type: string, providerId: string, format: "xlsx" | "csv") =>
  `${proj(`/outcomes/export/${type}`)}?format=${format}${providerId ? `&providerId=${providerId}` : ""}`;
export function uploadOutcomeFile(
  classId: string,
  type: ReportType,
  file: File,
  stageKey?: StageKey,
) {
  const f = new FormData();
  f.set("file", file);
  if (stageKey) f.set("stage", stageKey);
  return upload<{ lines: OutcomeLine[]; issues: { traineeCode: string; message: string }[] }>(
    proj(`/classes/${classId}/outcomes/${type}/parse`),
    f,
  );
}
export const submitReport = (
  classId: string,
  type: ReportType,
  body: { stageKey?: StageKey; lines: OutcomeLine[]; reasonForChange?: string },
  idempotencyKey: string,
) =>
  post<OutcomeReport>(proj(`/classes/${classId}/outcomes/${type}/submit`), body, {
    idempotencyKey,
  });
export const getReport = (id: string) => get<OutcomeReport>(proj(`/outcome-reports/${id}`));
export const listReports = (
  q: { status?: string; type?: string; needsApproval?: boolean; page?: number } = {},
) => get<Page<OutcomeReport>>(proj("/outcome-reports"), q);
export const returnReport = (id: string, reason: string) =>
  post<OutcomeReport>(proj(`/outcome-reports/${id}/return`), { reason });
export const discardReport = (id: string, reason: string) =>
  post<OutcomeReport>(proj(`/outcome-reports/${id}/discard`), { reason });
export const approveResubmission = (id: string, decision: "approve" | "reject", note: string) =>
  post<OutcomeReport>(proj(`/outcome-reports/${id}/approve-resubmission`), { decision, note });
export interface StatusRow {
  traineeId: string;
  code: string;
  name: string;
  providerCode: string;
  classCode: string;
  dropout: string;
  completion: string;
  certification: string;
  placement: string;
  locality: string;
  retention3m: string;
}
export const listOutcomeStatus = (q: {
  page?: number;
  providerId?: string;
  classId?: string;
  field?: string;
  value?: string;
  q?: string;
}) => get<Page<StatusRow>>(proj("/outcomes/status"), q);
export interface SiteContext {
  siteId: string;
  reportType: ReportType;
  label: string;
  providerCode: string;
  trainingTitle: string;
  locationName: string;
  classes: {
    id: string;
    code: string;
    state: "ready" | "accepted" | "waiting" | "blocked" | "empty";
    reason?: string;
    lines: OutcomeLine[];
  }[];
}
const site = (id: string, type: ReportType, end: string) =>
  proj(`/sites/${id}/outcomes/${type}/${end}`);
export const getSiteContext = (id: string, type: ReportType) =>
  get<SiteContext>(site(id, type, "context"));
export const siteTemplateUrl = (id: string, type: ReportType, format: "xlsx" | "csv") =>
  `${site(id, type, "template")}?format=${format}`;
export function parseSiteFile(id: string, type: ReportType, file: File) {
  const f = new FormData();
  f.set("file", file);
  return upload<{
    classes: { id: string; code: string; lines: OutcomeLine[] }[];
    issues: { traineeCode: string; message: string }[];
  }>(site(id, type, "parse"), f);
}
export const submitSite = (
  id: string,
  type: ReportType,
  body: { classes: { id: string; lines: OutcomeLine[] }[]; filename?: string },
  idempotencyKey: string,
) => post<{ accepted: number }>(site(id, type, "submit"), body, { idempotencyKey });
