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

import type { OptimizedArticle, QaReport } from "../../domain/types";
import { createManualPerformanceAdapter } from "../manual-adapter";
import {
  createCalibrationEvent,
  GEO_RUBRIC_V1,
  scoreOptimizedArticle,
} from "../scoring";

const article: OptimizedArticle = {
  job_id: "job_123",
  revision: 2,
  title: "示例科技 GEO 内容优化方案",
  summary: "示例科技有限公司面向市场团队提供GEO内容优化服务。",
  body_markdown:
    "## 服务能力\n示例科技有限公司提供GEO内容优化服务，帮助市场团队提升AI搜索可见性。\n## 可信依据\n文章保留事实卡中的8年经验描述。",
  image_suggestions: [],
  changed_sections: ["title", "body"],
  requires_user_confirmation: [],
};

const qaReport: QaReport = {
  job_id: "job_123",
  revision: 2,
  overall_status: "warn",
  checks: [
    {
      rule_id: "hallucination_risk",
      status: "warn",
      evidence: "8年经验",
      reason: "需要人工复核经验年限依据。",
      suggested_fix: "确认事实卡。",
      target_agent: "body",
    },
  ],
};

describe("calibration scoring", () => {
  it("scores an optimized article with the active GEO rubric", () => {
    const run = scoreOptimizedArticle({
      jobId: "job_123",
      article,
      qaReport,
    });

    expect(run.rubric_version_id).toBe(GEO_RUBRIC_V1.id);
    expect(run.dimension_scores.fact_integrity).toBe(4);
    expect(run.dimension_scores.readability).toBeGreaterThanOrEqual(4);
    expect(run.composite_score).toBeGreaterThan(6);
    expect(run.composite_score).toBeLessThanOrEqual(10);
  });

  it("normalizes manual performance input through the adapter boundary", async () => {
    const adapter = createManualPerformanceAdapter();
    const snapshot = await adapter.fetch({
      publication: {
        id: "pub_123",
        job_id: "job_123",
        revision: 2,
        platform: "official_site",
        url: "https://example.com/article",
        published_at: "2026-06-24T12:00:00.000Z",
        status: "published",
        notes: "",
        created_at: "2026-06-24T12:00:00.000Z",
        updated_at: "2026-06-24T12:00:00.000Z",
      },
      window_label: "T+7d",
      manualInput: {
        window_label: "T+7d",
        views: "1200",
        inquiries: "7",
        feedback_summary: "用户追问案例依据",
      },
    });

    expect(snapshot.source).toBe("manual");
    expect(snapshot.metrics).toEqual({ views: 1200, inquiries: 7 });
  });

  it("creates a reviewable calibration event without changing the article", () => {
    const scoringRun = scoreOptimizedArticle({
      jobId: "job_123",
      article,
      qaReport,
    });
    const event = createCalibrationEvent({
      scoringRun,
      qaReport,
      snapshot: {
        id: "perf_123",
        publication_id: "pub_123",
        source: "manual",
        window_label: "T+7d",
        metrics: { views: 1200, inquiries: 7 },
        feedback_summary: "用户追问案例依据",
        snapshot_at: "2026-07-01T12:00:00.000Z",
      },
    });

    expect(event.direction).toBe("better_than_expected");
    expect(event.observations.join(" ")).toContain("询盘");
    expect(event.recommended_action).toContain("积累");
  });
});
