import { describe, expect, it } from "vitest";
import {
  detectSponsorshipSignals,
  jobHasAnySponsorshipSignal,
  jobHasGreenSponsorshipFlag,
  jobHasRedSponsorshipFlag,
  jobHasSponsorshipBlocker,
  parseSponsorshipSignals,
  serializeSponsorshipSignals,
  sponsorshipSignalSearchHaystack,
} from "./sponsorship-signals";

describe("detectSponsorshipSignals", () => {
  it("detects TN / USMCA language", () => {
    expect(
      detectSponsorshipSignals({
        jobDescription: "We are open to TN candidates under USMCA.",
      }),
    ).toContain("tn_eligible");
  });

  it("detects TN-friendly and Canadian-welcome phrasing", () => {
    expect(
      detectSponsorshipSignals({
        jobDescription: "TN-friendly team. Open to Canadian citizens.",
      }),
    ).toContain("tn_eligible");
  });

  it("detects explicit sponsorship offers", () => {
    expect(
      detectSponsorshipSignals({
        jobDescription:
          "Visa sponsorship is available for qualified candidates.",
      }),
    ).toContain("will_sponsor");
  });

  it("detects authorized-to-work language", () => {
    expect(
      detectSponsorshipSignals({
        jobDescription:
          "Must be authorized to work in the U.S. No relocation assistance.",
      }),
    ).toEqual(
      expect.arrayContaining(["authorized_to_work", "no_citizenship_block"]),
    );
  });

  it("flags citizenship / GC / EAD-only requirements as red", () => {
    const signals = detectSponsorshipSignals({
      jobDescription: "US Citizen, GC, EAD only. Onsite in Boca Raton, FL.",
    });
    expect(signals).toContain("citizenship_required");
    expect(signals).not.toContain("no_citizenship_block");
    expect(signals).not.toContain("will_sponsor");
  });

  it("flags security clearance as red", () => {
    const signals = detectSponsorshipSignals({
      jobDescription:
        "Active Secret clearance required. QA automation lead in Arlington, VA.",
    });
    expect(signals).toContain("clearance_required");
    expect(signals).not.toContain("will_sponsor");
  });

  it("flags unrestricted work authorization as red", () => {
    const signals = detectSponsorshipSignals({
      jobDescription:
        "Must have unrestricted work authorization. SDET role in Dallas, TX.",
    });
    expect(signals).toContain("unrestricted_work_auth");
    expect(signals).not.toContain("authorized_to_work");
  });

  it("flags explicit no-sponsorship language as yellow", () => {
    const signals = detectSponsorshipSignals({
      jobDescription:
        "Great QA automation role. No sponsorship. Must work onsite in Austin, TX.",
    });
    expect(signals).toContain("no_sponsorship_stated");
    expect(signals).not.toContain("citizenship_required");
    expect(signals).not.toContain("will_sponsor");
  });

  it("does not treat citizen requirement as yellow no-sponsor only", () => {
    const signals = detectSponsorshipSignals({
      jobDescription: "Must be a U.S. citizen. No visa sponsorship available.",
    });
    expect(signals).toContain("citizenship_required");
    expect(signals).not.toContain("no_sponsorship_stated");
  });

  it("detects Canada + U.S. presence across company fields", () => {
    expect(
      detectSponsorshipSignals({
        location: "Toronto, ON",
        companyDescription: "Global team with offices in the United States.",
      }),
    ).toContain("ca_us_presence");
  });

  it("returns empty when no text is available", () => {
    expect(detectSponsorshipSignals({})).toEqual([]);
  });
});

describe("sponsorship signal serialization", () => {
  it("round-trips signal ids", () => {
    const serialized = serializeSponsorshipSignals([
      "tn_eligible",
      "will_sponsor",
    ]);
    expect(parseSponsorshipSignals(serialized)).toEqual([
      "tn_eligible",
      "will_sponsor",
    ]);
  });

  it("filters unknown ids when parsing", () => {
    expect(parseSponsorshipSignals('["tn_eligible","bogus"]')).toEqual([
      "tn_eligible",
    ]);
  });
});

describe("jobHasAnySponsorshipSignal", () => {
  it("matches when any required signal is present", () => {
    const json = serializeSponsorshipSignals(["authorized_to_work"]);
    expect(
      jobHasAnySponsorshipSignal(json, ["tn_eligible", "authorized_to_work"]),
    ).toBe(true);
    expect(jobHasAnySponsorshipSignal(json, ["tn_eligible"])).toBe(false);
  });
});

describe("flag tier helpers", () => {
  it("detects red and green flags separately", () => {
    const redJson = serializeSponsorshipSignals(["clearance_required"]);
    const greenJson = serializeSponsorshipSignals(["tn_eligible"]);
    const yellowJson = serializeSponsorshipSignals(["no_sponsorship_stated"]);

    expect(jobHasRedSponsorshipFlag(redJson)).toBe(true);
    expect(jobHasGreenSponsorshipFlag(redJson)).toBe(false);
    expect(jobHasGreenSponsorshipFlag(greenJson)).toBe(true);
    expect(jobHasRedSponsorshipFlag(greenJson)).toBe(false);
    expect(jobHasSponsorshipBlocker(yellowJson)).toBe(true);
    expect(jobHasRedSponsorshipFlag(yellowJson)).toBe(false);
  });
});

describe("sponsorshipSignalSearchHaystack", () => {
  it("includes labels and aliases for search", () => {
    const haystack = sponsorshipSignalSearchHaystack(
      serializeSponsorshipSignals(["tn_eligible"]),
    );
    expect(haystack).toContain("tn");
    expect(haystack).toContain("usmca");
  });
});
