import { describe, expect, it } from "vitest";
import { getPresets } from "./demoData";
import { createLocalAccountSnapshot } from "./localAccountSnapshot";
import type { StoredArtifactRecord } from "../types/storage";

function artifact(id: string, equipped: string, slot: string, setName = "Viridescent Venerer"): StoredArtifactRecord {
  return {
    id,
    name: `${slot} Piece`,
    slot,
    setName,
    mainStat: slot === "Flower of Life" ? "HP" : slot === "Plume of Death" ? "ATK" : "Elemental Mastery",
    mainValue: slot === "Flower of Life" ? "4,780" : slot === "Plume of Death" ? "311" : "187",
    substats: ["CRIT Rate+7.0%", "CRIT DMG+14.0%", "Energy Recharge+11.0%", "ATK+29"],
    equipped,
    confidence: 94,
    needsReview: false,
    source: "manual-scan",
  };
}

describe("localAccountSnapshot", () => {
  it("infers owned characters from equipped artifact records", () => {
    const snapshot = createLocalAccountSnapshot([
      artifact("a", "Sucrose", "Flower of Life"),
      artifact("b", "Sucrose", "Plume of Death"),
    ], getPresets());

    expect(snapshot?.characters.find((character) => character.name === "Sucrose")?.owned).toBe(true);
    expect(snapshot?.recommendations.length).toBeGreaterThan(0);
  });

  it("creates partial build suggestions for locally inferred characters", () => {
    const snapshot = createLocalAccountSnapshot([
      artifact("a", "Aino", "Flower of Life", "Silken Moon's Serenade"),
      artifact("b", "Aino", "Plume of Death", "Silken Moon's Serenade"),
      artifact("c", "Aino", "Sands of Eon", "Silken Moon's Serenade"),
    ], getPresets());

    const aino = snapshot?.characters.find((character) => character.name === "Aino");
    const ainoBuilds = snapshot?.builds.filter((build) => build.characterId === aino?.id) ?? [];

    expect(aino?.owned).toBe(true);
    expect(ainoBuilds.length).toBeGreaterThan(0);
    expect(ainoBuilds[0].warnings.join(" ")).toContain("Missing");
  });
});
