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

import type { ConfirmedFactCard } from "../../domain/types";
import { normalizeInput } from "../input-normalizer";
import { inspectQuality } from "../quality-inspector";

const confirmedFactCard: ConfirmedFactCard = {
  company_full_name: "Example Technology Co., Ltd.",
  company_short_names: ["Example Tech"],
  brand_names: ["Example"],
  product_names: ["Example GEO"],
  target_industry: "GEO optimization",
  target_audience: "Marketing teams",
  experience_years: 8,
  core_claims: ["Eight years of GEO optimization experience"],
  forbidden_claims: ["industry first"],
  image_topics: ["product dashboard"],
  uncertain_items: [],
  is_ready_for_optimization: true,
  confirmed_by_user: true,
};

describe("workflow nodes", () => {
  it("normalizes input whitespace and image lines", () => {
    const normalized = normalizeInput({
      title: "  A GEO Article  ",
      body: "\nFirst paragraph.\n\nSecond paragraph.  ",
      image_lines:
        " Product dashboard screenshot \n https://example.com/image.png \n\n",
      platform: "official_site",
      user_instructions: "  Keep factual. ",
    });

    expect(normalized.article_draft.title).toBe("A GEO Article");
    expect(normalized.article_draft.body).toBe(
      "First paragraph.\n\nSecond paragraph.",
    );
    expect(normalized.image_assets).toEqual([
      { type: "description", content: "Product dashboard screenshot" },
      { type: "link", content: "https://example.com/image.png" },
    ]);
  });

  it("returns the 10 required quality checks", () => {
    const report = inspectQuality({
      article: {
        title: "Example GEO Optimization Guide",
        summary: "A factual guide for marketing teams.",
        body_markdown:
          "Example Technology Co., Ltd. has eight years of GEO optimization experience.",
        image_suggestions: [],
        changed_sections: [],
        requires_user_confirmation: [],
      },
      factCard: confirmedFactCard,
      platform: "official_site",
      sourceImages: [],
    });

    expect(report.checks.map((check) => check.rule_id)).toEqual([
      "industry_alignment",
      "image_text_match",
      "voice_consistency",
      "platform_fit",
      "company_name_integrity",
      "title_quality",
      "body_quality",
      "hallucination_risk",
      "claim_consistency",
      "context_sensitive_terms",
    ]);
  });

  it("returns quality report details in Chinese", () => {
    const report = inspectQuality({
      article: {
        title: "Example GEO Optimization Guide",
        summary: "A factual guide for marketing teams.",
        body_markdown:
          "Example Technology Co., Ltd. has eight years of GEO optimization experience.",
        image_suggestions: [],
        changed_sections: [],
        requires_user_confirmation: [],
      },
      factCard: confirmedFactCard,
      platform: "official_site",
      sourceImages: [],
    });

    expect(report.checks[0]?.reason).toContain("行业");
    expect(report.checks[0]?.suggested_fix).toContain("行业");
    expect(report.checks[0]?.evidence).not.toContain("Article");
  });

  it("hard-fails incomplete company names and conflicting years while warning on industry drift and hallucination risk", () => {
    const report = inspectQuality({
      article: {
        title: "Example Wins Finance Automation Market!!!",
        summary: "A finance automation story.",
        body_markdown:
          "Example has 12 years of finance automation experience, 99 patents, and works in banking automation.",
        image_suggestions: [],
        changed_sections: [],
        requires_user_confirmation: [],
      },
      factCard: confirmedFactCard,
      platform: "official_site",
      sourceImages: [],
    });

    const failures = report.checks.filter((check) => check.status === "fail");
    expect(failures.map((check) => check.rule_id)).toEqual(
      expect.arrayContaining([
        "company_name_integrity",
        "claim_consistency",
      ]),
    );
    const industryCheck = report.checks.find(
      (check) => check.rule_id === "industry_alignment",
    );
    expect(industryCheck?.status).toBe("warn");
    const hallucinationCheck = report.checks.find(
      (check) => check.rule_id === "hallucination_risk",
    );
    expect(hallucinationCheck?.status).toBe("warn");
    expect(report.overall_status).toBe("fail");
  });

  it("does not flag numbers already present in confirmed fact card claims", () => {
    const report = inspectQuality({
      article: {
        title: "Example GEO Optimization Guide",
        summary: "Example Technology Co., Ltd. serves teams with 8 years of experience.",
        body_markdown:
          "Example Technology Co., Ltd. has 8 years of GEO optimization experience.",
        image_suggestions: [],
        changed_sections: [],
        requires_user_confirmation: [],
      },
      factCard: confirmedFactCard,
      platform: "official_site",
      sourceImages: [],
    });

    const hallucinationCheck = report.checks.find(
      (check) => check.rule_id === "hallucination_risk",
    );
    expect(hallucinationCheck?.status).toBe("pass");
  });

});
