import { describe, expect, it } from "vitest";
import {
  inferCountryKeysFromJobText,
  isVagueJobLocation,
  jobMatchesAllowedCountry,
  jobMatchesBlockedCountries,
  normalizeBlockedCountryTokens,
  resolveBlockedCountriesFromStoredString,
} from "./blocked-countries.js";

describe("blocked-countries", () => {
  it("normalizes country tokens to canonical keys", () => {
    expect(normalizeBlockedCountryTokens(["India", "UK", "bogus"])).toEqual([
      "india",
      "united kingdom",
    ]);
  });

  it("parses JSON and legacy comma-separated storage", () => {
    expect(
      resolveBlockedCountriesFromStoredString(JSON.stringify(["India"])),
    ).toEqual(["india"]);
    expect(resolveBlockedCountriesFromStoredString("india, Poland")).toEqual([
      "india",
      "poland",
    ]);
  });

  it("matches jobs whose location includes a blocked country", () => {
    const blocked = resolveBlockedCountriesFromStoredString('["india"]');
    expect(
      jobMatchesBlockedCountries("Bangalore, Karnataka, India", blocked),
    ).toBe(true);
    expect(jobMatchesBlockedCountries("Toronto, ON, Canada", blocked)).toBe(
      false,
    );
    expect(jobMatchesBlockedCountries("Remote", blocked)).toBe(false);
    expect(jobMatchesBlockedCountries(null, blocked)).toBe(false);
  });

  it("treats worldwide and remote-only locations as vague", () => {
    expect(isVagueJobLocation("Worldwide")).toBe(true);
    expect(isVagueJobLocation("Remote")).toBe(true);
    expect(isVagueJobLocation("Toronto, Canada")).toBe(false);
  });

  it("finds blocked countries in description when location is worldwide", () => {
    const blocked = resolveBlockedCountriesFromStoredString('["india"]');
    expect(
      jobMatchesBlockedCountries(
        {
          location: "Worldwide",
          jobDescription:
            "Job Location: Mumbai/Nagpur. We are hiring in India for this role.",
        },
        blocked,
      ),
    ).toBe(true);
    expect(
      jobMatchesBlockedCountries(
        {
          location: "Worldwide",
          jobDescription: "Fully remote team across North America.",
        },
        blocked,
      ),
    ).toBe(false);
  });

  it("infers country names embedded in free text", () => {
    expect(
      inferCountryKeysFromJobText(
        "Harrier is hiring in India. Job Location: Mumbai/Nagpur",
      ),
    ).toContain("india");
  });

  it("allows only jobs clearly in the selected search country", () => {
    expect(jobMatchesAllowedCountry("Toronto, ON, Canada", "canada")).toBe(
      true,
    );
    expect(jobMatchesAllowedCountry("Vancouver, BC", "canada")).toBe(true);
    expect(
      jobMatchesAllowedCountry("Bangalore, Karnataka, India", "canada"),
    ).toBe(false);
    expect(jobMatchesAllowedCountry("Remote", "canada")).toBe(false);
    expect(jobMatchesAllowedCountry("Worldwide", "canada")).toBe(false);
    expect(
      jobMatchesAllowedCountry(
        {
          location: "Remote",
          jobDescription: "Hiring in India only.",
        },
        "canada",
      ),
    ).toBe(false);
    expect(
      jobMatchesAllowedCountry(
        {
          location: "Remote",
          jobDescription: "Remote across Canada.",
        },
        "canada",
      ),
    ).toBe(true);
  });
});
