import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

import { afterEach, beforeEach, describe, expect, it } from "vitest";

import {
  aggregateSummary,
  redactSensitiveText,
  writeSampleResult,
  writeSummaryFiles,
} from "../e2e/sample-flow/reporting";
import type { InvalidArticleSample, SampleResult } from "../e2e/sample-flow/types";

describe("reporting helpers", () => {
  let reportDir: string;

  beforeEach(() => {
    reportDir = mkdtempSync(join(tmpdir(), "geo-report-"));
  });

  afterEach(() => {
    rmSync(reportDir, { recursive: true, force: true });
  });

  it("redacts API keys and authorization tokens", () => {
    const redacted = redactSensitiveText(
      'x-api-key: local-dev-key Authorization: Bearer secret DEEPSEEK_API_KEY="abc"',
      ["local-dev-key", "abc"],
    );

    expect(redacted).toBe(
      'x-api-key: [REDACTED] Authorization: Bearer [REDACTED] DEEPSEEK_API_KEY="[REDACTED]"',
    );
  });

  it("writes per-sample results and aggregate summary files", () => {
    const sample: SampleResult = {
      file: "samples/articles/title-quality.json",
      name: "Title quality",
      slug: "title-quality",
      status: "passed",
      duration_ms: 1234,
      job_id: "job_123",
      qa_status: "warn",
      qa_fail_rules: [],
      qa_warn_rules: ["title_quality"],
      expected_hard_failures: [],
      expected_warnings: ["title_quality"],
      exports: {
        "optimized.md": "passed",
        "optimized.docx": "passed",
        "qa_report.json": "passed",
      },
      llm_tasks: ["fact_extractor", "article_optimizer", "quality_inspector"],
      artifacts: {
        final_screenshot: "samples/title-quality/final.png",
      },
    };
    const invalid: InvalidArticleSample[] = [
      {
        filePath: "/repo/samples/articles/bad.json",
        fileName: "bad.json",
        reason: "input.body must be a non-empty string",
      },
    ];

    writeSampleResult(reportDir, sample);
    const summary = aggregateSummary({
      startedAt: "2026-07-01T00:00:00.000Z",
      finishedAt: "2026-07-01T00:01:00.000Z",
      provider: "deepseek",
      model: "deepseek-v4-pro",
      baseURL: "http://127.0.0.1:3000",
      reportDir,
      samples: [sample],
      invalidSamples: invalid,
    });
    writeSummaryFiles(reportDir, summary);

    expect(
      existsSync(join(reportDir, "samples", "title-quality", "result.json")),
    ).toBe(true);
    expect(
      JSON.parse(readFileSync(join(reportDir, "summary.json"), "utf8")),
    ).toEqual(
      expect.objectContaining({
        provider: "deepseek",
        totals: { passed: 1, failed: 0, skipped: 1 },
      }),
    );
    expect(readFileSync(join(reportDir, "summary.md"), "utf8")).toContain(
      "| Title quality | passed | warn |",
    );
  });
});
