import { expect, it } from "vitest";
import { canonicalHeader, checkTrainee, parseBool } from "./trainee.rules.js";
import { eligibility } from "./eligibility.rules.js";

const lookup = (_d: string, raw: string) => ({ "addis ababa": "addis_ababa", diploma: "diploma", "not currently working": "not_working" })[raw.toLowerCase()];
const good = { first_name: "Abebe", father_name: "Kebede", grandfather_name: "Tesfa", sex: "M", age: "24", date_of_birth: "14/11/2002", phone: "0911223344", region: "Addis Ababa", woreda: "05", education: "Diploma", employment: "Not currently working", class_code: "C1", roster_status: "Active", form_date: "15/05/2020", consent_accuracy: "Yes", consent_contact_verify: "Yes" };
const codes = (over: Record<string, string>) => checkTrainee({ ...good, ...over }, { lookup, requireExternalId: false }).issues.filter((i) => i.severity === "error").map((i) => i.code);

it("accepts a correct person", () => {
  const r = checkTrainee({ ...good, age: "17" }, { lookup, requireExternalId: false });
  expect(r.row["phone_e164"]).toBe("+251911223344");
  expect(r.row["date_of_birth"]).toBe("2002-11-14");
  expect(codes({ age: "17" })).toEqual([]);
});
it.each([["phone", "9.11E+08", "PHONE_SCIENTIFIC_NOTATION"], ["phone", "0811223344", "PHONE_INVALID"], ["form_date", "3/25/2020", "DATE_INVALID"], ["form_date", "01/01/2999", "DATE_FUTURE"], ["date_of_birth", "06/14/1995", "DOB_INVALID"], ["age", "40", "AGE_MISMATCH"], ["region", "Nowhere", "VALUE_UNKNOWN"], ["consent_accuracy", "maybe", "BOOLEAN_INVALID"], ["first_name", "R2D2", "NAME_INVALID"], ["roster_status", "x", "ROSTER_INVALID"], ["consent_accuracy", "No", "CONSENT_REFUSED"], ["consent_contact_verify", "", "REQUIRED"], ["woreda", "", "REQUIRED"]])("%s = %s is refused", (k, v, code) => expect(codes({ [k]: v, ...(k === "age" ? {} : { age: "17" }) })).toContain(code));
it("accepts V1 headers", () => expect(["firstName", "phone_primary", "dateOfFormCompletion", "Roster_Role"].map(canonicalHeader)).toEqual(["first_name", "phone", "form_date", "roster_status"]));
it("never turns an unclear answer into empty", () => expect([parseBool("yes"), parseBool("no"), parseBool(""), parseBool("maybe")]).toEqual([true, false, null, undefined]));
it("applies the protocol eligibility rule", () => {
  expect(eligibility({ age: 18, education: "diploma", employment: "not_working" }).status).toBe("eligible");
  expect(eligibility({ age: 30, education: "primary", employment: "full_time" }).reasons).toHaveLength(3);
  expect(eligibility({ age: null, education: "diploma", employment: "not_working" }).status).toBe("unknown");
});
