import { FOOTER_LAYOUTS } from "@/components/site-layouts";
import { useSiteSettings } from "@/contexts/SiteSettingsContext";

type Content = Record<string, unknown>;

const LEGACY_DEFAULT_TEXT = "Orion STEM Schools · Family Curriculum Portal · 2026–2027";

function str(value: unknown): string {
  return typeof value === "string" ? value : "";
}

/**
 * Page-level Landing footer block. When using site/global footer settings, renders the
 * configured Site Settings footer layout. The global layout shell omits its
 * footer when this block is present on the page.
 *
 * Closing Vision (`closing-vision`) is a separate Landing content section.
 * PBEL Guide Footer (`pbel-guide-footer`) is a separate flyer footer — do not
 * use this Landing Footer in place of it.
 */
export function FooterBlock({ content }: { content?: Content }) {
  const { settings } = useSiteSettings();
  const rawText = str(content?.text).trim();
  const hasCustomText = Boolean(rawText) && rawText !== LEGACY_DEFAULT_TEXT;
  const forceCustom = content?.useSiteFooter === false;
  const useGlobalFooter = !forceCustom && !hasCustomText;

  if (useGlobalFooter) {
    const footerKey = settings.footer_layout;
    const Footer =
      FOOTER_LAYOUTS[footerKey]?.Component ?? FOOTER_LAYOUTS.default.Component;
    return <Footer />;
  }

  const text = rawText || LEGACY_DEFAULT_TEXT;
  return (
    <footer className="py-10 bg-bark text-center mt-auto">
      <p className="font-display text-milk/60 text-sm">{text}</p>
    </footer>
  );
}
