import { describe, expect, it } from "vitest";
import { captureRejectionReason, captureSourceRejectionReason, shouldPersistParsedArtifact } from "./scannerCaptureQuality";
import type { CaptureResult } from "../types/global";
import type { ParsedArtifactCandidate } from "./artifactOcrParser";

function capture(overrides: Partial<CaptureResult> = {}): CaptureResult {
  return {
    id: "test",
    name: "test",
    width: 1920,
    height: 1080,
    dataUrl: "",
    capturedAt: new Date(0).toISOString(),
    captureTarget: "genshin-client",
    ocr: [],
    ...overrides,
  };
}

function parsed(overrides: Partial<ParsedArtifactCandidate> = {}): ParsedArtifactCandidate {
  return {
    name: "A Note in Spring's Leich",
    slot: "Sands of Eon",
    level: 20,
    mainStat: "Elemental Mastery",
    mainValue: "187",
    substats: ["ATK+29", "CRIT DMG+15.5%", "CRIT Rate+2.7%"],
    setName: "A Day Carved From Rising Winds",
    equipped: "Citlali",
    confidence: 92,
    notes: [],
    fields: {
      name: { value: "A Note in Spring's Leich", confidence: 92, source: "ocr" },
      slot: { value: "Sands of Eon", confidence: 92, source: "ocr" },
      level: { value: "20", confidence: 96, source: "ocr" },
      mainStat: { value: "Elemental Mastery", confidence: 92, source: "ocr" },
      mainValue: { value: "187", confidence: 92, source: "ocr" },
      setName: { value: "A Day Carved From Rising Winds", confidence: 92, source: "ocr" },
      equipped: { value: "Citlali", confidence: 92, source: "ocr" },
      substats: { value: "ATK+29, CRIT DMG+15.5%, CRIT Rate+2.7%", confidence: 92, source: "ocr" },
    },
    ...overrides,
  };
}

describe("scannerCaptureQuality", () => {
  it("rejects primary-screen fallback captures", () => {
    expect(captureRejectionReason(capture({ captureTarget: "primary-screen" }), parsed())).toContain("Primary Screen");
    expect(captureSourceRejectionReason(capture({ captureTarget: "primary-screen" }))).toContain("Primary Screen");
  });

  it("accepts a direct desktop/window capture when the OCR content looks like Genshin", () => {
    expect(captureSourceRejectionReason(capture({
      captureTarget: "desktop-source",
      ocr: [{ id: "artifact-title", label: "title", text: "Gladiator's Nostalgia\nFlower of Life", confidence: 90 }],
    }))).toBe("");
  });

  it("rejects captures that clearly contain the app UI", () => {
    expect(
      captureRejectionReason(
        capture({
          ocr: [{ id: "artifact-title", label: "title", text: "Artifacts scannen\nScanner Diagnose", confidence: 90 }],
        }),
        parsed(),
      ),
    ).toContain("App");
  });

  it("does not persist obviously incomplete review parses", () => {
    expect(shouldPersistParsedArtifact(parsed({ mainStat: "Unknown main stat" }), true)).toBe(false);
    expect(shouldPersistParsedArtifact(parsed({ substats: [] }), true)).toBe(false);
    expect(shouldPersistParsedArtifact(parsed({ confidence: 59 }), true)).toBe(false);
    expect(shouldPersistParsedArtifact(parsed({ confidence: 80 }), true)).toBe(true);
  });

  it("persists complete artifacts even if they do not need review", () => {
    expect(shouldPersistParsedArtifact(parsed(), false)).toBe(true);
  });
});
