import { expect, it } from "vitest";
import { checkPlacement, COLUMNS, SUPPORT } from "./placement.rules.js";

const support = Object.fromEntries(SUPPORT.map((k, i) => [k, ["Yes", "no", "TRUE", "0", "1", "N", "y"][i]!]));
const wage = { ...support, employment_type: "wage_employed", job_title: "IT support officer", employment_start_date: "2026-10-01", monthly_income_etb: "8500", contract_type: "full_time_permanent", employer_name: "ABC", employer_contact_person: "Abebe", employer_phone: "0911234567" };
const free = { ...support, employment_type: "self_employed_freelance", employment_start_date: "2026-10-01", monthly_income_etb: "4000.50", legal_form: "no_legal_form", has_business_license: "No" };
const run = (v: "yes" | "no", d: Record<string, string>) => checkPlacement(v, d, "2026-12-01");
const fields = (v: "yes" | "no", d: Record<string, string>) => run(v, d).problems.map((p) => p.field);
it("has the 29 columns in the agreed order", () => { expect(COLUMNS).toHaveLength(29); expect([COLUMNS[0], COLUMNS[4], COLUMNS[6], COLUMNS[13], COLUMNS[15], COLUMNS[20], COLUMNS[27], COLUMNS[28]]).toEqual(["external_beneficiary_id", "reported_value", "placement_support_outreach_referrals", "employment_type", "job_title", "employer_name", "has_business_license", "notes"]); });
it("accepts complete rows", () => { for (const d of [wage, free, { ...free, employment_type: "self_employed_business", legal_form: "plc" }, { ...support, employment_type: "other", employment_type_other_text: "Family farm", employment_start_date: "01/10/2026", monthly_income_etb: "1" }]) expect(run("yes", d).problems).toEqual([]); expect(run("no", support).problems).toEqual([]); });
it("reads Yes and No in every usual way", () => expect(SUPPORT.map((k) => run("no", support).clean[k])).toEqual(["yes", "no", "yes", "no", "yes", "no", "yes"]));
it.each([["job_title"], ["contract_type"], ["employer_name"], ["employer_contact_person"], ["monthly_income_etb"], ["employment_start_date"], ["placement_support_job_fair"], ["employment_type"]])("a wage row without %s fails", (k) => expect(fields("yes", { ...wage, [k]: "" })).toEqual([k]));
it.each([["legal_form"], ["has_business_license"], ["monthly_income_etb"]])("a self-employed row without %s fails", (k) => expect(fields("yes", { ...free, [k]: "" })).toEqual([k]));
it("needs one way to reach the employer", () => { const { employer_phone: _p, ...d } = wage; expect(fields("yes", d)).toEqual(["employer_phone"]); expect(fields("yes", { ...d, employer_email: "a@b.et" })).toEqual([]); });
it.each([
  ["no", { ...support, employer_name: "ABC" }, "employer_name"], ["yes", { ...wage, has_business_license: "No" }, "has_business_license"], ["yes", { ...wage, legal_form: "plc" }, "legal_form"],
  ["yes", { ...free, contract_type: "other" }, "contract_type"], ["yes", { ...free, employer_email: "a@b.et" }, "employer_email"], ["yes", { ...wage, contract_type: "other" }, "contract_type_other_text"], ["yes", { ...wage, contract_type_other_text: "x" }, "contract_type_other_text"],
  ["yes", { ...free, legal_form: "other" }, "legal_form_other_text"], ["yes", { ...wage, employment_type_other_text: "x" }, "employment_type_other_text"], ["yes", { ...wage, contract_type: "forever" }, "contract_type"],
  ["yes", { ...wage, employer_phone: "9.84E+08" }, "employer_phone"], ["yes", { ...wage, employer_email: "nobody" }, "employer_email"], ["yes", { ...wage, employment_start_date: "45200" }, "employment_start_date"], ["yes", { ...wage, employment_start_date: "10/25/2026" }, "employment_start_date"], ["yes", { ...wage, employment_start_date: "2026-12-02" }, "employment_start_date"],
  ["yes", { ...wage, monthly_income_etb: "8,500" }, "monthly_income_etb"], ["yes", { ...wage, monthly_income_etb: "8500 ETB" }, "monthly_income_etb"], ["yes", { ...wage, monthly_income_etb: "0" }, "monthly_income_etb"], ["no", { ...support, placement_support_job_fair: "maybe" }, "placement_support_job_fair"],
] as const)("%s row: a wrong value is refused (%#)", (v, d, f) => expect(fields(v, d as Record<string, string>)).toContain(f));
it("writes phone numbers one way", () => expect(["0911234567", "+251911234567", "251911234567", "911234567"].map((p) => run("yes", { ...wage, employer_phone: p }).clean["employer_phone"])).toEqual(Array(4).fill("+251911234567")));
