import { DEFAULT_CONTENT } from "@/app/(site)/_components/home/defaults";
import { CtaLink, Lines } from "@/app/(site)/_components/home/parts";
import type { FooterContent } from "@/app/(site)/_lib/content";

/**
 * "Copyright ©Unionists. All Right Reserved. Designed & Developed by Sofrik" → two lines,
 * as the design shows it. A "\n" in the text decides the break; without one, the last
 * sentence goes on its own line.
 */
function copyrightLines(text: string): string {
  if (text.includes("\n")) return text;
  const m = /^(.*\.)\s+([^.]+\.?)$/.exec(text.trim());
  return m ? `${m[1]}\n${m[2]}` : text;
}

/**
 * Site footer (was partials/footer.html). The home page passes the CMS footer
 * content; every other page shows the built-in content.
 */
export function SiteFooter({ content = DEFAULT_CONTENT.footer }: { content?: FooterContent }) {
  return (
    <>
      <footer className="site-footer">
        <div className="site-footer__inner">
          <div className="site-footer__grid">
            <div className="site-footer__brand">
              <div className="site-footer__brand-name">Unionists</div>
              {content.description && (
                <p className="site-footer__tagline">
                  <Lines text={content.description} />
                </p>
              )}
              {content.copyright && (
                <p className="site-footer__copyright">
                  <Lines text={copyrightLines(content.copyright)} />
                </p>
              )}
            </div>
            {content.linkGroups.map((group, i) => (
              <div key={i} className="footer-col">
                <h4 className="site-footer__col-title">{group.title}</h4>
                <ul className="site-footer__links">
                  {group.links.map((link, j) => (
                    <li key={j}>
                      <CtaLink cta={link} className="site-footer__link" />
                    </li>
                  ))}
                </ul>
              </div>
            ))}
            {content.socialLinks.length > 0 && (
              <div className="footer-col">
                <h4 className="site-footer__col-title">Social Links</h4>
                <ul className="site-footer__links">
                  {content.socialLinks.map((link, i) => (
                    <li key={i}>
                      <CtaLink cta={link} className="site-footer__link footer-ext-link">
                        {link.label}
                        <svg viewBox="0 0 12 12" width="10" height="10" aria-hidden="true">
                          <path
                            d="M2 10 L10 2 M5 2 L10 2 L10 7"
                            stroke="currentColor"
                            strokeWidth="1.6"
                            fill="none"
                            strokeLinecap="round"
                            strokeLinejoin="round"
                          ></path>
                        </svg>
                      </CtaLink>
                    </li>
                  ))}
                </ul>
              </div>
            )}
          </div>
        </div>
      </footer>
    </>
  );
}
