import { describe, expect, it } from "vitest";
import { storedArtifactsToDomain } from "./storedArtifactAdapter";
import type { StoredArtifactRecord } from "../types/storage";

function record(overrides: Partial<StoredArtifactRecord> = {}): StoredArtifactRecord {
  return {
    id: "stored-1",
    name: "A Note in Spring's Leich",
    slot: "Sands of Eon",
    level: 16,
    setName: "Viridescent Venerer",
    mainStat: "ATK",
    mainValue: "46.6%",
    substats: ["ATK+29", "ATK+15.2%", "CRIT DMG+15.5%", "Energy Recharge+11.7%"],
    equipped: "Sucrose",
    confidence: 96,
    needsReview: false,
    source: "auto-scan",
    lastSeenAt: "2026-07-04T00:00:00.000Z",
    ...overrides,
  };
}

describe("storedArtifactAdapter", () => {
  it("converts stored OCR artifacts into recommendation-domain artifacts", () => {
    const [artifact] = storedArtifactsToDomain([record()]);

    expect(artifact.slot).toBe("sands");
    expect(artifact.setKey).toBe("viridescent_venerer");
    expect(artifact.mainStat).toBe("ATK%");
    expect(artifact.level).toBe(16);
    expect(artifact.equipped).toBe("Sucrose");
    expect(artifact.confidence).toBe(0.96);
    expect(artifact.source).toBe("screen");
  });

  it("keeps flat and percent ATK substats distinct", () => {
    const [artifact] = storedArtifactsToDomain([record()]);

    expect(artifact.substats).toEqual(
      expect.arrayContaining([
        { key: "ATK", value: 29, unit: "flat" },
        { key: "ATK%", value: 15.2, unit: "%" },
      ]),
    );
  });
});
