import test from "node:test";
import assert from "node:assert/strict";
import {
  findSystemFinalDropoutViolations,
  systemFinalDropoutRejection,
} from "./dropoutPolicy.js";
import type { DropoutProjectionRow } from "./dropoutProtocol.js";

function proj(over: Partial<DropoutProjectionRow> & { traineeId: bigint }): DropoutProjectionRow {
  return {
    traineeUniqueId: `U-${over.traineeId}`,
    traineeName: `Trainee ${over.traineeId}`,
    trainingProviderId: 6n,
    trainingId: 1n,
    cohortId: null,
    possibleToDate: 10,
    presentToDate: 10,
    attendancePctToDate: 100,
    possibleFinal: 10,
    presentFinal: 10,
    attendancePctFinal: 100,
    maxRecoverablePct: 100,
    systemDropoutRisk: false,
    systemDropoutFinal: false,
    tpReportedDropout: false,
    disputedSystemFinal: false,
    effectiveDropout: false,
    reasonCode: null,
    reasonSource: null,
    missingInTpReport: false,
    ...over,
  };
}

const systemFinal = (id: bigint, pct: number) =>
  proj({
    traineeId: id,
    attendancePctFinal: pct,
    systemDropoutFinal: true,
    effectiveDropout: true,
    reasonCode: "low_attendance_auto",
    reasonSource: "system_auto",
  });

test("system-final trainee with no dropout row is reported as missing", () => {
  const violations = findSystemFinalDropoutViolations([], [systemFinal(1n, 73.33)]);
  assert.equal(violations.length, 1);
  assert.equal(violations[0]!.issue, "missing");
  assert.equal(violations[0]!.traineeId, "1");
  assert.equal(violations[0]!.attendancePctFinal, 73.33);
  assert.equal(violations[0]!.traineeUniqueId, "U-1");
});

test("system-final trainee reported N is a violation, not a dispute", () => {
  const violations = findSystemFinalDropoutViolations(
    [{ traineeId: "1", outcomeType: "dropout", reportedValue: false }],
    [systemFinal(1n, 30)],
  );
  assert.equal(violations.length, 1);
  assert.equal(violations[0]!.issue, "reported_no");
});

test("system-final trainee reported Y passes", () => {
  const violations = findSystemFinalDropoutViolations(
    [{ traineeId: "1", outcomeType: "dropout", reportedValue: true }],
    [systemFinal(1n, 30)],
  );
  assert.equal(violations.length, 0);
});

test("CSV import rows carry bigint traineeIds and are matched by String()", () => {
  const violations = findSystemFinalDropoutViolations(
    [
      { traineeId: 73n, outcomeType: "dropout", reportedValue: true },
      { traineeId: 185n, outcomeType: "dropout", reportedValue: false },
    ],
    [systemFinal(73n, 70), systemFinal(185n, 30), systemFinal(932n, 50)],
  );
  assert.deepEqual(
    violations.map((v) => [v.traineeId, v.issue]),
    [
      ["185", "reported_no"],
      ["932", "missing"],
    ],
  );
});

test("non-dropout rows and null reported values never satisfy the policy", () => {
  const violations = findSystemFinalDropoutViolations(
    [
      { traineeId: "1", outcomeType: "completion", reportedValue: true },
      { traineeId: "2", outcomeType: "dropout", reportedValue: null },
    ],
    [systemFinal(1n, 40), systemFinal(2n, 50)],
  );
  assert.equal(violations.length, 2);
  assert.deepEqual(violations.map((v) => v.issue), ["missing", "missing"]);
});

test("trainees that are not system-final are ignored entirely", () => {
  const violations = findSystemFinalDropoutViolations(
    [],
    [
      proj({ traineeId: 1n, attendancePctFinal: 95 }),
      proj({ traineeId: 2n, attendancePctFinal: 70, systemDropoutFinal: false, systemDropoutRisk: true }),
    ],
  );
  assert.equal(violations.length, 0);
});

test("rejection payload counts both issues and caps the trainee list at 100", () => {
  const many = Array.from({ length: 120 }, (_, i) => systemFinal(BigInt(i + 1), 50));
  const violations = findSystemFinalDropoutViolations(
    [{ traineeId: "1", outcomeType: "dropout", reportedValue: false }],
    many,
  );
  const payload = systemFinalDropoutRejection(violations);
  assert.equal(payload.details.totalCount, 120);
  assert.equal(payload.details.reportedNoCount, 1);
  assert.equal(payload.details.missingCount, 119);
  assert.equal(payload.details.trainees.length, 100);
  assert.match(payload.error, /dropout=yes/);
});
