import { describe, expect, it } from "vitest";
import {
  pickBestRateCardLineMatch,
  scoreRateCardLineMatch,
} from "../router/rate-card-match-support.js";

describe("rate card match support", () => {
  it("scores weighted matches with optional case-insensitive keys", () => {
    expect(scoreRateCardLineMatch({
      roleId: "role_1",
      chapter: "Delivery",
      seniority: "Senior",
    }, {
      criteria: {
        roleId: "role_1",
        chapter: "delivery",
        seniority: "senior",
      },
      weights: {
        roleId: 4,
        chapter: 2,
        seniority: 1,
      },
      caseInsensitiveKeys: ["chapter", "seniority"],
    })).toEqual({
      line: {
        roleId: "role_1",
        chapter: "Delivery",
        seniority: "Senior",
      },
      score: 7,
      mismatch: false,
    });
  });

  it("marks mismatches when a populated criterion conflicts with a populated line field", () => {
    expect(scoreRateCardLineMatch({
      roleId: "role_pm",
      chapter: "Project Management",
    }, {
      criteria: {
        roleId: "role_3d",
        chapter: "Project Management",
      },
      weights: {
        roleId: 4,
        chapter: 2,
      },
    })).toEqual({
      line: {
        roleId: "role_pm",
        chapter: "Project Management",
      },
      score: 2,
      mismatch: true,
    });
  });

  it("picks the highest-scoring non-mismatching line", () => {
    const lines = [
      { id: "generic", roleId: null, chapter: null, location: null, seniority: null, workType: null },
      { id: "specific", roleId: "role_3d", chapter: "Delivery", location: null, seniority: null, workType: null },
    ];

    expect(pickBestRateCardLineMatch(lines, {
      criteria: {
        roleId: "role_3d",
        chapter: "Delivery",
      },
      weights: {
        roleId: 4,
        chapter: 2,
      },
    })).toEqual(lines[1]);
  });
});
