import { describe, expect, it } from "vitest";
import type { CaptureResult } from "../types/global";
import { artifactTabClickTarget, buildAutoScanEntryPlan, keyPressBlocked, validateAutoScanEntryPreflight } from "./autoScanEntry";

function capture(overrides: Partial<CaptureResult> = {}): CaptureResult {
  return {
    id: "test",
    name: "test",
    width: 1920,
    height: 1080,
    dataUrl: "",
    capturedAt: new Date(0).toISOString(),
    inventoryGrid: {
      centers: [{ x: 179, y: 254, row: 0, col: 0 }],
      rows: 4,
      cols: 8,
      confidence: 76,
      source: "detected",
    },
    captureTarget: "genshin-client",
    artifactDetail: { present: true, confidence: 84, orangeHits: 14, greenHits: 8, textHits: 30 },
    layout: { aspect: "1.78:1", isSixteenNine: true, warning: "" },
    ...overrides,
  };
}

describe("autoScanEntry", () => {
  it("plans only read-only keys/clicks for the Paimon entry", () => {
    expect(buildAutoScanEntryPlan("paimon-menu")).toEqual([
      { type: "key", key: "ESC", label: "Leave the already-open Paimon menu" },
      { type: "key", key: "ESC", label: "If Paimon is still visible, close it before opening inventory" },
      { type: "key", key: "B", label: "Open inventory from world" },
      { type: "click-artifact-tab", label: "Select artifact inventory tab" },
      { type: "click-first-artifact", label: "Select first visible artifact so the detail card is open" },
      { type: "capture-preflight", label: "Verify artifact inventory grid and detail card" },
    ]);
  });

  it("plans a guided auto entry with direct world path before ESC/B fallback", () => {
    expect(buildAutoScanEntryPlan("auto-entry")).toEqual([
      { type: "key", key: "B", label: "Try direct inventory entry from world" },
      { type: "click-artifact-tab", label: "Select artifact inventory tab if inventory opened" },
      { type: "click-first-artifact", label: "Select first visible artifact so the detail card is open" },
      { type: "key", key: "ESC", label: "Fallback: leave the already-open Paimon menu" },
      { type: "key", key: "ESC", label: "Fallback: close Paimon menu if ESC opened or left it visible" },
      { type: "key", key: "B", label: "Fallback: open inventory from world" },
      { type: "click-artifact-tab", label: "Fallback: select artifact inventory tab" },
      { type: "click-first-artifact", label: "Fallback: select first visible artifact" },
      { type: "capture-preflight", label: "Verify artifact inventory grid and detail card" },
    ]);
  });

  it("uses the artifact-tab coordinate ratio", () => {
    expect(artifactTabClickTarget(capture())).toEqual({ x: 672, y: 47 });
  });

  it("blocks invalid lookup and unsupported layouts before auto-scan", () => {
    expect(validateAutoScanEntryPreflight(capture(), { valid: false, errors: ["bad"], warnings: [], summary: { artifactSets: 0, artifactPieces: 0, characters: 0, stats: 0, generatedAt: "", sourceVersion: "" } }).reason).toContain("Lookup invalid");
    expect(validateAutoScanEntryPreflight(capture({ layout: { aspect: "2.39:1", isSixteenNine: false, warning: "nicht 16:9" } })).reason).toContain("nicht 16:9");
  });

  it("blocks primary-screen and missing detail-card starts", () => {
    expect(validateAutoScanEntryPreflight(capture({ captureTarget: "primary-screen" })).reason).toContain("Primary Screen");
    expect(validateAutoScanEntryPreflight(capture({ artifactDetail: { present: false, confidence: 12, orangeHits: 0, greenHits: 1, textHits: 4 } })).reason).toContain("Keine Artifact-Detailansicht");
  });

  it("blocks the Paimon menu before grid clicks or OCR", () => {
    expect(validateAutoScanEntryPreflight(capture({
      artifactDetail: {
        present: false,
        confidence: 8,
        orangeHits: 12,
        greenHits: 154,
        textHits: 20,
        titleOrangeHits: 4,
        upperTextHits: 11,
        lowerGreenHits: 0,
      },
      paimonMenu: {
        present: true,
        confidence: 81,
        profileLightPct: 56,
        profileCreamPct: 31.1,
        menuTileDarkPct: 67.7,
      },
    })).reason).toContain("Paimon-Menue erkannt");
  });

  it("detects blocked key input", () => {
    expect(keyPressBlocked({ ok: false, key: "B", inputBlocked: true })).toBe(true);
    expect(keyPressBlocked({ ok: true, key: "B", inputBlocked: false })).toBe(false);
  });
});
