import { describe, expect, it } from "vitest";
import { deriveChain } from "./status.rules.js";

const all = (input: Parameters<typeof deriveChain>[0]) => Object.values(deriveChain(input)).map((d) => d.status);

describe("status is known to the last indicator", () => {
  it("100 trainees, 10 dropouts: the 10 are 'no' everywhere, the 90 wait for the provider", () => {
    const people = Array.from({ length: 100 }, (_, i) => deriveChain({ dropout: i < 10 ? "yes" : "no" }));
    expect(people.filter((p) => p.completion.status === "no")).toHaveLength(10);
    expect(people.filter((p) => p.completion.status === "pending")).toHaveLength(90);
    expect(people[0]!.retention).toEqual({ status: "no", source: "derived_dropout" });
  });
  it("a non-completer is 'no' for everything that needs completion", () => {
    expect(all({ dropout: "no", completion: "no" })).toEqual(["no", "no", "no", "no", "no"]);
    expect(deriveChain({ dropout: "no", completion: "no" }).placement.source).toBe("derived_certification");
  });
  it("not certified settles placement and everything after it", () => expect(all({ dropout: "no", completion: "yes", certification: "no" })).toEqual(["yes", "no", "no", "no", "no"]));
  it("not placed settles locality and retention", () => {
    const d = deriveChain({ completion: "yes", certification: "yes", placement: "no" });
    expect([d.retention, d.locality]).toEqual([{ status: "no", source: "derived_placement" }, { status: "no", source: "derived_placement" }]);
  });
  it("a reported value always wins and nothing is guessed without a report", () => {
    expect(deriveChain({ dropout: "yes", completion: "yes" }).completion).toEqual({ status: "yes", source: "tp_reported" });
    expect(all({})).toEqual(Array(5).fill("pending"));
  });
  it("correcting a dropout to 'no' makes the trainee pending again", () => expect(all({ dropout: "no" })).toEqual(Array(5).fill("pending")));
});
