import { describe, expect, it } from "vitest";
import {
  FOOTER_BLOCK_SLUG,
  PBEL_GUIDE_FOOTER_BLOCK_SLUG,
  sectionsHaveFooterBlock,
} from "./pageHasFooterBlock";
import {
  normalizePageSettings,
  resolveShowFooter,
  shouldHideGlobalFooter,
  shouldRenderFooterBlock,
} from "./pageFooterSettings";
import type { PageSection } from "@/admin/types";

function sectionWithBlocks(
  blocks: Array<{ componentId: number; componentName: string; componentSlug?: string }>,
): PageSection {
  return {
    width: "full",
    layout: "100",
    blocks: blocks.map((b) => ({ ...b, content: {} })),
  };
}

describe("sectionsHaveFooterBlock", () => {
  it("detects the Landing Footer block slug", () => {
    expect(FOOTER_BLOCK_SLUG).toBe("footer");
    expect(
      sectionsHaveFooterBlock([
        sectionWithBlocks([{ componentId: 4, componentName: "Footer", componentSlug: "footer" }]),
      ]),
    ).toBe(true);
  });

  it("detects the PBEL Guide Footer block slug", () => {
    expect(PBEL_GUIDE_FOOTER_BLOCK_SLUG).toBe("pbel-guide-footer");
    expect(
      sectionsHaveFooterBlock([
        sectionWithBlocks([
          { componentId: 99, componentName: "PBEL Guide Footer", componentSlug: "pbel-guide-footer" },
        ]),
      ]),
    ).toBe(true);
  });

  it("does not treat Closing Vision as the page footer", () => {
    expect(
      sectionsHaveFooterBlock([
        sectionWithBlocks([
          { componentId: 60, componentName: "Closing Vision", componentSlug: "closing-vision" },
        ]),
      ]),
    ).toBe(false);
  });

  it("keeps Closing Vision as content when a Footer block follows", () => {
    expect(
      sectionsHaveFooterBlock([
        sectionWithBlocks([
          { componentId: 60, componentName: "Closing Vision", componentSlug: "closing-vision" },
        ]),
        sectionWithBlocks([{ componentId: 4, componentName: "Footer", componentSlug: "footer" }]),
      ]),
    ).toBe(true);
  });
});

describe("pageFooterSettings", () => {
  const emptySections: PageSection[] = [];
  const footerSections = [
    sectionWithBlocks([{ componentId: 4, componentName: "Footer", componentSlug: "footer" }]),
  ];

  it("defaults showFooter to ON", () => {
    expect(resolveShowFooter({})).toBe(true);
    expect(resolveShowFooter(null)).toBe(true);
    expect(resolveShowFooter(normalizePageSettings({ footerType: "pbel-guide-footer" }))).toBe(true);
    expect(resolveShowFooter({ showFooter: false })).toBe(false);
  });

  it("hides global footer when Show Default Footer is OFF", () => {
    expect(shouldHideGlobalFooter(emptySections, { showFooter: false })).toBe(true);
    expect(shouldHideGlobalFooter(emptySections, { showFooter: true })).toBe(false);
  });

  it("still hides global footer when a Footer block is present and toggle is ON", () => {
    expect(shouldHideGlobalFooter(footerSections, { showFooter: true })).toBe(true);
  });

  it("suppresses footer blocks when Show Default Footer is OFF", () => {
    expect(shouldRenderFooterBlock("footer", { showFooter: false })).toBe(false);
    expect(shouldRenderFooterBlock("pbel-guide-footer", { showFooter: false })).toBe(false);
    expect(shouldRenderFooterBlock("hero", { showFooter: false })).toBe(true);
    expect(shouldRenderFooterBlock("footer", { showFooter: true })).toBe(true);
  });
});
