import Link from "next/link";
import { localizedPath, type Locale } from "@alterra/shared";
import Logo from "./Logo";
import BrandSignature from "./brand/BrandSignature";
import MarkWatermark from "./brand/MarkWatermark";
import { getBrandCopy } from "@/lib/brand";
import NewsletterForm from "./NewsletterForm";
import { CookiePreferencesButton } from "./consent/ConsentProvider";
import { getConsentCopy } from "@/lib/consentCopy";
import { contactInfo, whatsappUrl } from "@/lib/i18n";
import { getFormMessages, type LayoutCopy, type Menus } from "@/lib/site";

const socials = ["[INSTAGRAM]", "[LINKEDIN]", "[YOUTUBE]"];

export default function Footer({ lang, copy, menus }: { lang: Locale; copy: LayoutCopy; menus: Menus }) {
  const to = (path: string) => localizedPath(lang, path);
  const f = copy.footer;

  const columns = [
    { title: f.services, links: menus.services.map((s) => ({ label: s.name, path: s.path })) },
    { title: f.locations, links: menus.locations.map((l) => ({ label: l.name, path: l.path })) },
    {
      title: f.company,
      links: [
        { label: copy.nav.about, path: "/about" },
        { label: f.advantage, path: "/about/alterra-advantage" },
        { label: f.sell, path: "/sell-with-us" },
        { label: copy.nav.insights, path: "/insights" },
        { label: copy.nav.contact, path: "/contact" },
      ],
    },
    {
      title: f.legal,
      links: [
        { label: f.privacy, path: "/privacy-policy" },
        { label: f.terms, path: "/terms" },
        { label: f.legalNotice, path: "/legal-notice" },
      ],
    },
  ];

  return (
    <footer className="relative overflow-hidden bg-ink text-cream/65">
      <MarkWatermark position="top-right" />
      <div className="relative mx-auto max-w-7xl px-5 pt-20 md:px-8">
        <div className="grid gap-12 border-b border-cream/10 pb-14 lg:grid-cols-[1.3fr_1fr]">
          <div>
            <Link href={to("/")} className="inline-block">
              <Logo variant="stacked" height={120} className="h-24 md:h-28" />
            </Link>
            <BrandSignature text={getBrandCopy(lang).signature} className="mt-8 text-gold-light" />
            <p className="mt-3 max-w-md font-serif text-2xl leading-snug text-cream/90">{copy.promise}</p>
          </div>
          <NewsletterForm
            title={f.newsletterTitle}
            placeholder={f.newsletterPlaceholder}
            button={f.newsletterButton}
            thanks={f.newsletterThanks}
            lang={lang}
            messages={getFormMessages(lang)}
          />
        </div>

        <div className="grid grid-cols-2 gap-x-8 gap-y-12 py-14 md:grid-cols-3 lg:grid-cols-5">
          {columns.map((col) => (
            <nav key={col.title} aria-label={col.title}>
              <p className="eyebrow text-[0.62rem] text-cream/70">{col.title}</p>
              <ul className="mt-5 space-y-3 text-sm">
                {col.links.map((link) => (
                  <li key={link.path}>
                    <Link href={to(link.path)} className="transition hover:text-gold-light">
                      {link.label}
                    </Link>
                  </li>
                ))}
                {col.title === f.legal && (
                  <li>
                    <CookiePreferencesButton label={getConsentCopy(lang).preferences} />
                  </li>
                )}
              </ul>
            </nav>
          ))}

          <div className="col-span-2 md:col-span-3 lg:col-span-1">
            <p className="eyebrow text-[0.62rem] text-cream/70">{f.contactTitle}</p>
            <ul className="mt-5 space-y-3 text-sm">
              <li>{contactInfo.phoneDisplay}</li>
              <li>
                <a href={whatsappUrl()} target="_blank" rel="noopener" className="transition hover:text-gold-light">
                  WhatsApp {contactInfo.whatsapp ? `+${contactInfo.whatsapp}` : "[WHATSAPP]"}
                </a>
              </li>
              <li>{contactInfo.email}</li>
              <li>{contactInfo.address}</li>
            </ul>
            <p className="eyebrow mt-8 text-[0.62rem] text-cream/70">{f.follow}</p>
            <ul className="mt-4 flex flex-wrap gap-x-4 gap-y-2 text-xs">
              {socials.map((s) => (
                <li key={s}>{s}</li>
              ))}
            </ul>
          </div>
        </div>
      </div>

      <div className="relative border-t border-cream/10">
        <div className="mx-auto flex max-w-7xl flex-col gap-3 px-5 py-6 pb-24 text-xs text-cream/70 md:flex-row md:items-center md:justify-between md:px-8 md:pb-6">
          <p>
            © {new Date().getFullYear()} Alterra Estates. {f.rights}
          </p>
          <p>
            {f.partnership}{" "}
            <a href="https://mifinka.com" target="_blank" rel="noopener" className="text-cream/80 underline underline-offset-4 transition hover:text-gold-light">
              Mifinka
            </a>
          </p>
        </div>
      </div>
    </footer>
  );
}
