import { eq, inArray } from "drizzle-orm";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { db } from "../db";
import { searchProfiles } from "../db/schema";
import {
  getSearchProfileIdForBasicAuthUser,
  listProfilesForBasicAuthUser,
  profileMatchesBasicAuthUser,
  setActiveSearchProfileIdForBasicAuthUser,
} from "./profiles";

const CA_PROFILE_ID = "685b0000-0000-4000-8000-000000009901";
const US_PROFILE_ID = "685b0000-0000-4000-8000-000000009902";

const sharedProfileData = {
  targetRoles: ["SDET"],
  experienceLevel: "Senior",
  mustHaveSkills: [],
  niceToHaveSkills: [],
  dealBreakers: [],
  preferredWorkArrangement: [],
  preferredLocations: [],
  minimumSalary: "",
  industriesToTarget: [],
  industriesToAvoid: [],
  aboutMe: "",
  basicAuthUser: "__test_profiles_repo__",
};

describe("profiles repository", () => {
  afterEach(async () => {
    await db
      .delete(searchProfiles)
      .where(inArray(searchProfiles.id, [CA_PROFILE_ID, US_PROFILE_ID]));
  });

  beforeEach(async () => {
    await db
      .delete(searchProfiles)
      .where(inArray(searchProfiles.id, [CA_PROFILE_ID, US_PROFILE_ID]));
    await db.insert(searchProfiles).values([
      {
        id: CA_PROFILE_ID,
        name: "Test Profiles Repo CA",
        data: JSON.stringify({
          ...sharedProfileData,
          preferredLocations: ["Canada"],
          resumeLocalPath: "../data/resumes/ilia-dobkin.json",
        }),
      },
      {
        id: US_PROFILE_ID,
        name: "Test Profiles Repo US",
        data: JSON.stringify({
          ...sharedProfileData,
          preferredLocations: ["United States"],
          resumeLocalPath: "../data/resumes/ilia-dobkin-us.json",
        }),
      },
    ]);
  });

  it("matches comma-separated basic auth aliases", () => {
    expect(profileMatchesBasicAuthUser("ilia,dobkin", "ILIA")).toBe(true);
    expect(profileMatchesBasicAuthUser("ilia,dobkin", "dobkin")).toBe(true);
    expect(profileMatchesBasicAuthUser("ilia,dobkin", "cherepaha")).toBe(false);
  });

  it("lists every profile visible to the login", async () => {
    const visible = await listProfilesForBasicAuthUser(
      "__test_profiles_repo__",
    );
    expect(visible.map((profile) => profile.id).sort()).toEqual(
      [CA_PROFILE_ID, US_PROFILE_ID].sort(),
    );
  });

  it("defaults to the first matching profile when none is stored", async () => {
    await expect(
      getSearchProfileIdForBasicAuthUser("__test_profiles_repo__"),
    ).resolves.toBe(CA_PROFILE_ID);
  });

  it("remembers the active profile across basic auth aliases", async () => {
    await db
      .update(searchProfiles)
      .set({
        data: JSON.stringify({
          ...sharedProfileData,
          preferredLocations: ["Canada"],
          basicAuthUser: "test-repo-a,test-repo-b",
        }),
      })
      .where(eq(searchProfiles.id, US_PROFILE_ID));

    await setActiveSearchProfileIdForBasicAuthUser(
      "test-repo-a",
      US_PROFILE_ID,
    );

    await expect(
      getSearchProfileIdForBasicAuthUser("test-repo-a"),
    ).resolves.toBe(US_PROFILE_ID);
    await expect(
      getSearchProfileIdForBasicAuthUser("test-repo-b"),
    ).resolves.toBe(US_PROFILE_ID);
  });
});
