import Link from "next/link";
import { cn } from "cn";
import type { Locale } from "@/i18n/config";
import type { Dictionary } from "@/i18n/get-dictionary";
import { navLinks, routes } from "@/lib/routes";
import { LogoStacked } from "../logo";
import { FooterLinks } from "./footer-links";
import { NewsletterForm } from "./newsletter-form";
import { SocialLinks } from "./social-links";

type FooterProps = {
  locale: Locale;
  t: Dictionary["footer"];
  brandName: string;
  /** Page-specific spacing above the footer (Figma differs per page). */
  className?: string;
};

const columnTitle = "font-display text-xs leading-[1.1] font-semibold text-white md:text-base lg:text-lg";

export function Footer({ locale, t, brandName, className }: FooterProps) {
  const links = navLinks(locale)
    .filter((l) => l.key !== "home")
    .map(({ key, href }) => ({ key, href, label: t.links[key as keyof typeof t.links] }));

  return (
    <footer className={cn("mt-[108px] border-t border-soft/30 bg-footer md:mt-12 lg:mt-[151px]", className)}>
      {/*
        Phone (Figma 390): a grid — logo with "Join Us" under it on the start side, Quick Links on the end side,
        "Subscribe" across the bottom (the last column uses display:contents so its two blocks become grid items).
        Tablet (744): 3 columns centred vertically. Desktop: 3 columns, top-aligned.
      */}
      <div className="container-site grid grid-cols-[1fr_auto] gap-y-5 py-5 md:flex md:flex-row md:items-start md:justify-between md:max-lg:items-center md:max-lg:py-10 lg:py-20">
        <Link href={routes.home(locale)} aria-label={brandName} className="shrink-0 justify-self-start max-md:mb-[5px] max-lg:[zoom:0.75]">
          <LogoStacked />
        </Link>

        <FooterLinks title={t.quickLinks} titleClassName={columnTitle} links={links} className="max-md:col-start-2 max-md:row-span-2 max-md:row-start-1 max-md:self-end lg:w-[270px]" />

        <div className="contents md:flex md:flex-col md:gap-10 md:max-lg:w-[335px] md:max-lg:gap-5 lg:w-[440px]">
          {/* Tablet: Figma keeps 37px between the icons and "Subscribe" (a fixed 77px block + 20px gap). */}
          <div className="flex flex-col gap-2.5 max-md:col-start-1 max-md:row-start-2 md:gap-[18px] md:max-lg:pb-[17px]">
            <h2 className={columnTitle}>{t.joinUs}</h2>
            <SocialLinks />
          </div>
          <div className="flex flex-col gap-2 max-md:col-span-2">
            <h2 className={`${columnTitle} md:max-lg:text-sm`}>{t.subscribeTitle}</h2>
            <NewsletterForm t={t} />
          </div>
        </div>
      </div>
    </footer>
  );
}
