import { slugFromComponentName } from "@/lib/blockSlug";

/**
 * PBEL family-flyer blocks that stack tightly inside one page shell
 * (reference: /course/pbel-water — single max-w-4xl mx-auto px-6 py-10).
 */
export const PBEL_FLYER_BLOCK_SLUGS = [
  "orion-school-header",
  "what-is-pbel",
  "why-water-why-now",
  "pbel-cycle",
  "what-your-child-might-do",
  "families-expect-help",
  "benefits-for-your-child",
  "service-learning-ipard",
  "year-timeline-at-glance",
  "how-families-can-help",
  "deep-inquiry-questions",
  "stay-in-the-loop",
] as const;

const PBEL_FLYER_SLUG_SET = new Set<string>(PBEL_FLYER_BLOCK_SLUGS);

/**
 * Blocks that already include `container mx-auto px-6` (or BrandscapeShell /
 * full-bleed heroes). CMS sections wrapping these must use width "full" to
 * avoid double padding / wrong max-width vs brandscape-explore-main.
 */
export const SELF_CONTAINED_BLOCK_SLUGS = new Set([
  // Full-bleed / wide container (~1400px)
  "hero",
  "dimension-detail",
  "grade-family-guides",
  "grade-family-guides-api",
  "grade-hero",
  "grade-kaleidoscope",
  "curriculum-hero",
  "essential-question",
  "core-courses",
  "dna-strands",
  "year-at-glance",
  "year-long-story",
  "developing-the-whole-child",
  "finks-taxonomy",
  "one-project-not-13-tests",
  "curriculum-architecture",
  "pbel",
  "course-hero",
  "quarter-hero",
  "big-idea",
  "how-each-course-sees",
  "quarter-courses",
  "quarter-learning-tabs",
  "quarter-courses-and-learning",
  "footer",
  "parent-course-overview",
  // Course body — BrandscapeShell content (max-w-5xl)
  "why-this-course-matters",
  "how-connects",
  "smarter-assessment",
  "what-your-child-will-gain",
  "course-portrait",
  "course-core-skills",
  "course-journey",
  "course-what-they-create",
  "course-inquiry-questions",
  "course-support-at-home",
  "course-quarter-celebration",
  // PBEL flyer body — BrandscapeShell narrow (max-w-4xl)
  ...PBEL_FLYER_BLOCK_SLUGS,
  // PBEL Framework — Design to Impact (self-contained sections)
  "design-to-impact-hero",
  "impact-kaleidoscope",
  "three-journey-phases",
  "cross-cutting-mirrors",
  "annual-flow-quarters",
  "diverge-converge-viz",
  "squad-roles",
  "grade-band-entrepreneurship",
  "deliverables-evidence",
  "design-impact-summit",
  // Landing — closing content section (not the site footer)
  "closing-vision",
  // PBEL flyer page footer (replaces global footer when present / selected)
  "pbel-guide-footer",
  "club-category-grid",
  "steering-committees-grid",
  "orion-competitions",
  "public-competitions",
  "competition-tabs",
  "swot-analysis-grid",
  "root-cause-fishbone",
  "portrait-goals-grid",
  "growth-radar-chart",
  "progress-how-it-works",
  "progress-sample-report",
]);

export function resolvePageBlockSlug(block: {
  componentSlug?: string;
  componentName?: string;
}): string {
  if (block.componentSlug?.trim()) return block.componentSlug.trim();
  if (block.componentName?.trim()) return slugFromComponentName(block.componentName.trim());
  return "";
}

/** True when every rendered block on the page is a PBEL flyer fragment (footer optional). */
export function isPbelFlyerPage(sections: { blocks?: unknown[] }[]): boolean {
  const slugs: string[] = [];
  for (const section of sections) {
    for (const block of section.blocks ?? []) {
      if (!block || typeof block !== "object") continue;
      const b = block as { componentSlug?: string; componentName?: string; componentId?: unknown };
      if (!("componentId" in b || "componentSlug" in b || "componentName" in b)) continue;
      const slug = resolvePageBlockSlug(b);
      if (!slug) continue;
      slugs.push(slug);
    }
  }
  if (slugs.length === 0) return false;
  const hasFlyer = slugs.some((s) => PBEL_FLYER_SLUG_SET.has(s));
  if (!hasFlyer) return false;
  return slugs.every(
    (s) => PBEL_FLYER_SLUG_SET.has(s) || s === "footer" || s === "pbel-guide-footer",
  );
}

/** Section width that matches brandscape: self-contained blocks need full-bleed sections. */
export function sectionWidthForBlock(block: {
  componentSlug?: string;
  componentName?: string;
}): "full" | "container" {
  const slug = resolvePageBlockSlug(block);
  return SELF_CONTAINED_BLOCK_SLUGS.has(slug) ? "full" : "container";
}

/**
 * When a single-column section holds one self-contained block, force full width
 * at render time so saved pages that still say "container" do not double-pad.
 */
export function resolveEffectiveSectionWidth(section: {
  width: "full" | "container";
  layout?: string;
  blocks?: unknown[];
}): "full" | "container" {
  const layout = section.layout ?? "100";
  if (layout !== "100") return section.width;

  const blocks = (section.blocks ?? []).filter(
    (b): b is { componentSlug?: string; componentName?: string } =>
      Boolean(b) && typeof b === "object" && ("componentSlug" in b || "componentName" in b || "componentId" in b),
  );
  if (blocks.length !== 1) return section.width;
  if (sectionWidthForBlock(blocks[0]) === "full") return "full";
  return section.width;
}
