import { describe, expect, it } from "vitest";
import type { ParsedArtifactCandidate } from "./artifactOcrParser";
import type { IkArtifactCatalog } from "./ikArtifactMatcher";
import { matchParsedArtifactToIk } from "./ikArtifactMatcher";

const catalog: IkArtifactCatalog = {
  artifacts: [
    {
      normalizedName: "silkenmoonsserenade",
      setName: "Silken Moon's Serenade",
      good: "SilkenMoonsSerenade",
      pieces: [
        {
          slot: "plume",
          artifactName: "Pristine Plume of the Blessed",
          good: "PristinePlumeOfTheBlessed",
          normalizedName: "pristineplumeoftheblessed",
        },
        {
          slot: "flower",
          artifactName: "Blooming Warmth of the Blessed",
          good: "BloomingWarmthOfTheBlessed",
          normalizedName: "bloomingwarmthoftheblessed",
        },
      ],
    },
  ],
};

function parsedArtifact(overrides: Partial<ParsedArtifactCandidate> = {}): ParsedArtifactCandidate {
  const field = { value: "", confidence: 92, source: "ocr" as const };
  return {
    name: "Pristine Plume of the Blessed",
    slot: "Plume of Death",
    level: 20,
    mainStat: "ATK",
    mainValue: "311",
    substats: ["CRIT DMG+7.0%"],
    setName: "Silken Moon's Serenade",
    equipped: "Aino",
    confidence: 93,
    notes: [],
    fields: {
      name: field,
      slot: field,
      level: field,
      mainStat: field,
      mainValue: field,
      setName: field,
      equipped: field,
      substats: field,
    },
    ...overrides,
  };
}

describe("IK artifact matcher", () => {
  it("matches parsed artifact identity to IK GOOD keys", () => {
    const match = matchParsedArtifactToIk(parsedArtifact(), catalog);

    expect(match).toMatchObject({
      matched: true,
      confidence: 100,
      source: "ik-inventorylists",
      setKey: "silkenmoonsserenade",
      setGood: "SilkenMoonsSerenade",
      artifactKey: "pristineplumeoftheblessed",
      artifactGood: "PristinePlumeOfTheBlessed",
      slotKey: "plume",
      notes: [],
    });
  });

  it("flags IK slot mismatches for review", () => {
    const match = matchParsedArtifactToIk(parsedArtifact({ slot: "Flower of Life" }), catalog);

    expect(match?.matched).toBe(false);
    expect(match?.notes.join(" ")).toContain("slot mismatch");
  });

  it("keeps missing IK catalog data unsafe", () => {
    const match = matchParsedArtifactToIk(parsedArtifact(), { artifacts: [] });

    expect(match).toMatchObject({
      matched: false,
      confidence: 0,
      notes: ["IK artifact catalog unavailable."],
    });
  });
});
