import { describe, expect, it } from "vitest";
import type { PageSection } from "@/admin/types";
import {
  HOW_CONNECTS_LEGACY_HEADING,
  buildContentPlaceholderVars,
  resolveBlockContentForRender,
} from "@/lib/contentPlaceholders";

const mathPageSections = [
  {
    id: "s1",
    layout: "1",
    blocks: [
      {
        componentId: 1,
        componentName: "Course Hero",
        componentSlug: "course-hero",
        content: { name: "Mathematics", emoji: "📐" },
      },
      {
        componentId: 2,
        componentName: "How Connects to All Learning",
        componentSlug: "how-connects",
        content: {
          subject: "Science",
          heading: HOW_CONNECTS_LEGACY_HEADING,
          body: "Science provides the CONTENT KNOWLEDGE at the heart of the Project Spine. Every quarter's driving question starts with a scientific phenomenon that connects math, literacy, humanities, and all other subjects.",
          thisAddsText:
            "Science provides the WHY — the driving questions, investigations, and evidence that give purpose to counting, reading, mapping, and designing.",
        },
      },
    ],
  },
] as PageSection[];

describe("contentPlaceholders", () => {
  it("prefers Course Hero name over stale block subject fields for page vars", () => {
    const vars = buildContentPlaceholderVars(mathPageSections);
    expect(vars.subject).toBe("Mathematics");
    expect(vars.course_name).toBe("Mathematics");
  });

  it("normalizes legacy how-connects Science copy for non-Science courses", () => {
    const pageVars = buildContentPlaceholderVars(mathPageSections);
    const block = mathPageSections[0].blocks[1];
    const resolved = resolveBlockContentForRender((block.content ?? {}) as Record<string, unknown>, pageVars, {
      blockSlug: "how-connects",
    });

    expect(resolved.heading).toBe("How Mathematics Connects to All Learning");
    expect(resolved.body).toContain("Mathematics provides the CONTENT KNOWLEDGE");
    expect(resolved.body).not.toContain("Science provides");
    expect(resolved.thisAddsText).toContain("Mathematics provides the WHY");
  });

  it("uses fallback subject when course name is missing", () => {
    const resolved = resolveBlockContentForRender(
      { heading: "How {subject} Connects to All Learning" },
      {},
      { blockSlug: "how-connects" }
    );
    expect(resolved.heading).toBe("How This Subject Connects to All Learning");
  });

  it("resolves {subject} in Course Hero name from page title", () => {
    const sections = [
      {
        id: "s1",
        layout: "1",
        blocks: [
          {
            componentId: 1,
            componentName: "Course Hero",
            componentSlug: "course-hero",
            content: { name: "{subject}", weeklyTimeK5: "225 minutes" },
          },
        ],
      },
    ] as PageSection[];

    const pageVars = buildContentPlaceholderVars(sections, { pageTitle: "Decision Science" });
    const resolved = resolveBlockContentForRender(
      (sections[0].blocks[0].content ?? {}) as Record<string, unknown>,
      pageVars,
      { blockSlug: "course-hero" }
    );

    expect(pageVars.subject).toBe("Decision Science");
    expect(resolved.name).toBe("Decision Science");
    expect(resolved.weeklyTimeK5).toBe("225 minutes");
  });

  it("clears stale Science Course Hero name on non-Science pages", () => {
    const resolved = resolveBlockContentForRender(
      { name: "Science", weeklyTimeK5: "150 min" },
      { subject: "Mathematics", course_name: "Mathematics" },
      { blockSlug: "course-hero" }
    );

    expect(resolved.name).toBe("Mathematics");
    expect(resolved.weeklyTimeK5).toBe("150 minutes");
  });
});
