import Link from "next/link";
import { getSetting } from "@/lib/settings";
import SocialIcons from "@/components/SocialIcons";

// A value like "fb.com" (no protocol) is a valid relative URL as far as
// the browser is concerned, so it resolves against the current page —
// clicking it took people to yoursite.com/fb.com instead of fb.com. Any
// admin-entered link gets normalized to an absolute URL before it's ever
// used as an href.
function toAbsoluteUrl(value: string): string {
  const trimmed = value.trim();
  if (!trimmed) return "";
  if (/^https?:\/\//i.test(trimmed) || trimmed.startsWith("//")) return trimmed;
  return `https://${trimmed}`;
}

export default async function Footer() {
  const siteName = await getSetting("siteName", "GraceMembership");
  const llcName = await getSetting("companyLlcName", `${siteName} LLC`);
  const llcUrl = toAbsoluteUrl(await getSetting("companyLlcUrl", ""));
  const footerFontSize = await getSetting("footerFontSize", "sm");
  const year = new Date().getFullYear();

  const socialLinks = {
    facebook: toAbsoluteUrl(await getSetting("socialFacebook", "")),
    tiktok: toAbsoluteUrl(await getSetting("socialTiktok", "")),
    youtube: toAbsoluteUrl(await getSetting("socialYoutube", "")),
    whatsapp: toAbsoluteUrl(await getSetting("socialWhatsapp", "")),
    twitter: toAbsoluteUrl(await getSetting("socialTwitter", "")),
    linkedin: toAbsoluteUrl(await getSetting("socialLinkedin", "")),
  };
  const socialIconColor = await getSetting("socialIconColor", "#dc2626");
  const socialIconSize = await getSetting("socialIconSize", "md");

  // Tailwind needs full class names present in source for its scanner to
  // pick them up — a plain template string like `text-${size}` wouldn't
  // reliably generate the CSS, so map to the literal classes instead.
  const sizeClass: Record<string, string> = { xs: "text-xs", sm: "text-sm", base: "text-base", lg: "text-lg" };
  const textSize = sizeClass[footerFontSize] || "text-sm";

  const links = [
    { label: "Home", href: "/" },
    { label: "My Library", href: "/my-library" },
    { label: "About", href: "/about" },
    { label: "Contact", href: "/contact" },
    { label: "Terms and Conditions", href: "/terms" },
    { label: "Privacy Policy", href: "/privacy" },
    { label: "Refund Policy", href: "/refund-policy" },
  ];

  return (
    <footer className="border-t border-neutral-800 mt-16 pt-8 pb-10 px-4">
      <div className="max-w-6xl mx-auto flex flex-col sm:flex-row items-center justify-between gap-4">
        <div className={`${textSize} text-neutral-500`}>
          <span className="font-bold text-white">{siteName.charAt(0)}</span>
          <span className="text-neutral-400">{siteName.slice(1)}</span>
          <span className="mx-2">·</span>
          © {year}{" "}
          {llcUrl ? (
            <a href={llcUrl} target="_blank" rel="noopener noreferrer" className="underline hover:text-red-500">
              {llcName}
            </a>
          ) : (
            <span>{llcName}</span>
          )}
          . All rights reserved.
        </div>
        <nav className={`flex flex-wrap items-center justify-center gap-x-4 gap-y-2 ${textSize} text-neutral-400`}>
          {links.map((l, i) => (
            <span key={l.href} className="flex items-center gap-4">
              <Link href={l.href} className="hover:text-white transition-colors">
                {l.label}
              </Link>
              {i < links.length - 1 && <span className="text-neutral-700">|</span>}
            </span>
          ))}
        </nav>
      </div>
      <div className="flex justify-center mt-6">
        <SocialIcons links={socialLinks} color={socialIconColor} size={socialIconSize} />
      </div>
    </footer>
  );
}
