import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";

function writeCandidatePayload(dir: string) {
  const inputPath = path.join(dir, "review-eval-candidates.json");
  writeFileSync(
    inputPath,
    JSON.stringify({
      summary: {},
      candidates: [
        {
          id: "review-2026-07-08T15-02-39-765Z-152",
          savedAt: "2026-07-08T15:02:39.765Z",
          reason: "automatic:parser-notes:p1:r2c3",
          resolution: "1920x1080",
          ocr: {
            "artifact-name": "Pristine Plume of the Blessed",
            "artifact-slot": "Plume of Death",
            "artifact-main-stat-label": "ATK",
            "artifact-level": "+20",
            "artifact-substats": "- Energy Recharge+10.4%",
          },
        },
      ],
    }),
    "utf8",
  );
  return inputPath;
}

describe("prepare confirmed review case script", () => {
  it("creates a confirmed corpus snippet from a candidate and explicit labels", () => {
    const dir = mkdtempSync(path.join(tmpdir(), "gaa-confirmed-review-"));
    try {
      const inputPath = writeCandidatePayload(dir);
      const expectPath = path.join(dir, "expect.json");
      const outputPath = path.join(dir, "snippet.ts");
      writeFileSync(
        expectPath,
        `\uFEFF${JSON.stringify({
          name: "Pristine Plume of the Blessed",
          slot: "Plume of Death",
          level: 20,
          mainStat: "ATK",
          setName: "Silken Moon's Serenade",
        })}`,
        "utf8",
      );

      execFileSync(
        "node",
        [
          "scripts/prepare-confirmed-review-case.cjs",
          `--input=${inputPath}`,
          "--candidate=review-2026-07-08T15-02-39-765Z-152",
          `--expect-file=${expectPath}`,
          `--out=${outputPath}`,
        ],
        { cwd: process.cwd(), stdio: "pipe" },
      );

      const snippet = readFileSync(outputPath, "utf8");
      expect(snippet).toContain("confirmed: true");
      expect(snippet).toContain("Pristine Plume of the Blessed");
      expect(snippet).toContain('"artifact-name": "Pristine Plume of the Blessed"');
      expect(snippet).toContain("sourceCandidate=review-2026-07-08T15-02-39-765Z-152");
      expect(snippet).not.toContain("confirmed: false");
    } finally {
      rmSync(dir, { recursive: true, force: true });
    }
  });

  it("rejects missing explicit labels", () => {
    const dir = mkdtempSync(path.join(tmpdir(), "gaa-confirmed-review-"));
    try {
      const inputPath = writeCandidatePayload(dir);
      expect(() =>
        execFileSync(
          "node",
          ["scripts/prepare-confirmed-review-case.cjs", `--input=${inputPath}`, "--candidate=review-2026-07-08T15-02-39-765Z-152"],
          { cwd: process.cwd(), stdio: "pipe" },
        ),
      ).toThrow();
    } finally {
      rmSync(dir, { recursive: true, force: true });
    }
  });
});
