import { describe, expect, it } from "vitest";
import type { ParsedArtifactCandidate } from "./artifactOcrParser";
import {
  hashId,
  isReviewOnlyArtifactSource,
  resolveStoredArtifactSource,
  sessionSignature,
  storeSignature,
  storedArtifactStrength,
  toStoredArtifact,
} from "./artifactStore";

function candidate(overrides: Partial<ParsedArtifactCandidate> = {}): ParsedArtifactCandidate {
  const field = { value: "", confidence: 90, source: "ocr" as const };
  return {
    name: "Gladiator's Nostalgia",
    slot: "Flower of Life",
    level: 20,
    mainStat: "HP",
    mainValue: "4,780",
    substats: ["CRIT Rate+3.9%", "ATK%+5.8%", "Energy Recharge+5.2%", "ATK+19"],
    setName: "Gladiator's Finale",
    equipped: "Hu Tao",
    confidence: 91,
    notes: [],
    fields: {
      name: field,
      slot: field,
      level: field,
      mainStat: field,
      mainValue: field,
      setName: field,
      equipped: field,
      substats: field,
    },
    ...overrides,
  };
}

describe("artifactStore", () => {
  it("keeps the session signature sensitive to the equipped character", () => {
    const a = sessionSignature(candidate());
    const b = sessionSignature(candidate({ equipped: "Xiangling" }));
    expect(a).not.toBe(b);
  });

  it("ignores the equipped character in the store signature", () => {
    const a = storeSignature(candidate());
    const b = storeSignature(candidate({ equipped: "Xiangling" }));
    expect(a).toBe(b);
  });

  it("changes the store signature when substats change", () => {
    const a = storeSignature(candidate());
    const b = storeSignature(candidate({ substats: ["CRIT Rate+7.8%", "ATK%+5.8%", "Energy Recharge+5.2%", "ATK+19"] }));
    expect(a).not.toBe(b);
  });

  it("changes the store signature when the level changes", () => {
    const a = storeSignature(candidate({ level: 20 }));
    const b = storeSignature(candidate({ level: 16 }));
    expect(a).not.toBe(b);
  });

  it("produces a stable id for the same artifact", () => {
    const recordA = toStoredArtifact(candidate(), "auto-scan", false);
    const recordB = toStoredArtifact(candidate({ equipped: "Xiangling" }), "manual-scan", true);
    expect(recordA.id).toBe(recordB.id);
    expect(recordA.id).toBe(hashId(storeSignature(candidate())));
  });

  it("copies parsed values into the stored record", () => {
    const record = toStoredArtifact(candidate(), "auto-scan", true);
    expect(record.name).toBe("Gladiator's Nostalgia");
    expect(record.slot).toBe("Flower of Life");
    expect(record.level).toBe(20);
    expect(record.setName).toBe("Gladiator's Finale");
    expect(record.substats).toHaveLength(4);
    expect(record.needsReview).toBe(true);
    expect(record.source).toBe("auto-scan");
  });

  it("treats review recovery sources as lower priority than verified scan sources", () => {
    expect(isReviewOnlyArtifactSource("review-reprocess")).toBe(true);
    expect(resolveStoredArtifactSource("auto-scan", "review-reprocess")).toBe("auto-scan");
    expect(resolveStoredArtifactSource("review-recovered", "manual-scan")).toBe("manual-scan");
  });

  it("scores confirmed artifacts above uncertain review-only records", () => {
    const review = toStoredArtifact(candidate({ confidence: 74 }), "review-reprocess", true);
    const confirmed = toStoredArtifact(candidate({ confidence: 92 }), "auto-scan", false);
    expect(storedArtifactStrength(confirmed)).toBeGreaterThan(storedArtifactStrength(review));
  });
});
