/**
 * Storefront footer (bare skeleton) — fully driven by the merchant's
 * dashboard configuration.
 *
 * The merchant configures the footer in the Brainerce dashboard under
 *   Sell → Content → Footer
 *
 * Renders a brand block (store-name + social links), the merchant's link
 * columns, and a copyright bar.
 *
 * API contract:
 *   GET → client.content.footer.get('main', locale) → Content<'FOOTER'> | null
 *   Returns null on 404 — we render a minimal fallback so the layout never
 *   crashes when the merchant hasn't seeded the footer yet.
 */
import * as React from 'react';
// `Link` is for the fixed internal link (brand → /). The merchant-configured link
// columns go through `SmartLink`: it keeps the locale prefix and soft-navigates for
// an internal path, and renders a plain <a> for an external URL, mailto: or tel:.
// The social links stay plain <a target="_blank">: they always leave the site.
import { Link, SmartLink } from '@/core/lib/navigation';
import { NewsletterSignup } from './newsletter-signup';
import type { Content } from 'brainerce';

interface SiteFooterProps {
  /** Pre-fetched footer payload (server-side). `null` triggers fallback rendering. */
  footer: Content<'FOOTER'> | null;
  /** Store name shown in the brand block + fallback copyright. */
  storeName?: string;
}

export function SiteFooter({ footer, storeName }: SiteFooterProps) {
  const data = footer?.data;
  const columns = data?.columns ?? [];
  const social = data?.social ?? [];
  const year = new Date().getFullYear();
  const brandLabel = storeName ?? 'Store';

  if (!data || (columns.length === 0 && !data.copyright && social.length === 0)) {
    return (
      <footer>
        {/* DESIGN ME — footer (fallback branch): copyright only; Content<'FOOTER'> pre-fetched in the layout. Compose with the shadcn/ui primitives in src/components/ui (Button, Card, Badge, Input, Select, Accordion, Dialog, Skeleton, ...) + lucide-react icons. */}
        <p>
          © {year} {brandLabel}. <%- language === 'he' ? 'כל הזכויות שמורות.' : 'All rights reserved.' %>
        </p>
        <NewsletterSignup />
      </footer>
    );
  }

  return (
    <footer>
      {/* DESIGN ME — footer: brand block, merchant link columns and social links (Content<'FOOTER'> pre-fetched in the layout), copyright bar. Compose with the shadcn/ui primitives in src/components/ui (Button, Card, Badge, Input, Select, Accordion, Dialog, Skeleton, ...) + lucide-react icons. */}
      <div>
        <Link href="/">{brandLabel}</Link>
        {social.length > 0 ? (
          <ul className="flex flex-wrap items-center gap-2">
            {social.map((entry, idx) => (
              <li key={`${entry.platform}-${idx}`}>
                <a href={entry.url} target="_blank" rel="noopener noreferrer" aria-label={entry.platform}>
                  {entry.platform}
                </a>
              </li>
            ))}
          </ul>
        ) : null}
      </div>

      {columns.length > 0 ? (
        <div className="flex flex-wrap gap-8">
          {columns.map((col, idx) => (
            <nav key={`${col.title}-${idx}`} aria-label={col.title}>
              <h3>{col.title}</h3>
              <ul>
                {col.links.map((link, linkIdx) => (
                  <li key={`${link.url}-${linkIdx}`}>
                    <SmartLink href={link.url}>{link.label}</SmartLink>
                  </li>
                ))}
              </ul>
            </nav>
          ))}
        </div>
      ) : null}

      <NewsletterSignup />

      <p>{data.copyright ?? `© ${year} ${brandLabel}. <%- language === 'he' ? 'כל הזכויות שמורות.' : 'All rights reserved.' %>`}</p>
    </footer>
  );
}
