import Link from "next/link";
import type { ComponentType, SVGProps } from "react";
import { CONTACT } from "@/lib/vulai-content";
import { PLATFORM } from "@/lib/platform-content";
import { getSocialLinks } from "@db/queries/settings";
import BrandLogo from "@/components/brand-logo";

type IconProps = SVGProps<SVGSVGElement>;

// Brand glyphs kept inline so the footer needs no icon dependency. Each draws
// with currentColor so the buttons can tint to the platform's brand colour on
// hover.
const SocialIcons: Record<string, ComponentType<IconProps>> = {
  Instagram: (props) => (
    <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden {...props}>
      <path d="M12 2.16c3.2 0 3.58.01 4.85.07 1.17.05 1.8.25 2.23.41.56.22.96.48 1.38.9.42.42.68.82.9 1.38.16.42.36 1.06.41 2.23.06 1.27.07 1.65.07 4.85s-.01 3.58-.07 4.85c-.05 1.17-.25 1.8-.41 2.23-.22.56-.48.96-.9 1.38-.42.42-.82.68-1.38.9-.42.16-1.06.36-2.23.41-1.27.06-1.65.07-4.85.07s-3.58-.01-4.85-.07c-1.17-.05-1.8-.25-2.23-.41a3.7 3.7 0 0 1-1.38-.9 3.7 3.7 0 0 1-.9-1.38c-.16-.42-.36-1.06-.41-2.23C2.17 15.58 2.16 15.2 2.16 12s.01-3.58.07-4.85c.05-1.17.25-1.8.41-2.23.22-.56.48-.96.9-1.38.42-.42.82-.68 1.38-.9.42-.16 1.06-.36 2.23-.41C8.42 2.17 8.8 2.16 12 2.16Zm0 1.62c-3.15 0-3.52.01-4.76.07-1.15.05-1.77.24-2.19.4-.55.22-.94.47-1.35.88-.41.41-.66.8-.88 1.35-.16.42-.35 1.04-.4 2.19-.06 1.24-.07 1.61-.07 4.76s.01 3.52.07 4.76c.05 1.15.24 1.77.4 2.19.22.55.47.94.88 1.35.41.41.8.66 1.35.88.42.16 1.04.35 2.19.4 1.24.06 1.61.07 4.76.07s3.52-.01 4.76-.07c1.15-.05 1.77-.24 2.19-.4.55-.22.94-.47 1.35-.88.41-.41.66-.8.88-1.35.16-.42.35-1.04.4-2.19.06-1.24.07-1.61.07-4.76s-.01-3.52-.07-4.76c-.05-1.15-.24-1.77-.4-2.19a3.6 3.6 0 0 0-.88-1.35 3.6 3.6 0 0 0-1.35-.88c-.42-.16-1.04-.35-2.19-.4-1.24-.06-1.61-.07-4.76-.07Zm0 2.76a5.3 5.3 0 1 1 0 10.6 5.3 5.3 0 0 1 0-10.6Zm0 1.62a3.68 3.68 0 1 0 0 7.36 3.68 3.68 0 0 0 0-7.36Zm5.48-2.9a1.24 1.24 0 1 1 0 2.48 1.24 1.24 0 0 1 0-2.48Z" />
    </svg>
  ),
  YouTube: (props) => (
    <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden {...props}>
      <path d="M23.5 6.2a3.02 3.02 0 0 0-2.12-2.14C19.5 3.55 12 3.55 12 3.55s-7.5 0-9.38.51A3.02 3.02 0 0 0 .5 6.2C0 8.08 0 12 0 12s0 3.92.5 5.8a3.02 3.02 0 0 0 2.12 2.14c1.88.51 9.38.51 9.38.51s7.5 0 9.38-.51a3.02 3.02 0 0 0 2.12-2.14C24 15.92 24 12 24 12s0-3.92-.5-5.8ZM9.6 15.6V8.4l6.25 3.6-6.25 3.6Z" />
    </svg>
  ),
  TikTok: (props) => (
    <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden {...props}>
      <path d="M16.6 5.82a4.28 4.28 0 0 1-1.06-2.82h-3.3v13.1a2.59 2.59 0 0 1-2.59 2.5 2.59 2.59 0 0 1 0-5.18c.27 0 .53.04.77.12V9.9a5.97 5.97 0 0 0-.77-.05 5.9 5.9 0 1 0 5.9 5.9V8.98a7.55 7.55 0 0 0 4.42 1.42V7.1a4.28 4.28 0 0 1-3.37-1.28Z" />
    </svg>
  ),
  X: (props) => (
    <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden {...props}>
      <path d="M18.9 2.5h3.3l-7.2 8.24L23.5 21.5h-6.63l-5.2-6.79-5.94 6.79H2.43l7.7-8.8L1.5 2.5h6.8l4.7 6.21 5.9-6.21Zm-1.16 17h1.83L7.34 4.4H5.38L17.74 19.5Z" />
    </svg>
  ),
  Facebook: (props) => (
    <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden {...props}>
      <path d="M24 12a12 12 0 1 0-13.88 11.85v-8.38H7.08V12h3.04V9.36c0-3 1.79-4.67 4.53-4.67 1.31 0 2.68.24 2.68.24v2.95h-1.51c-1.49 0-1.95.92-1.95 1.87V12h3.32l-.53 3.47h-2.79v8.38A12 12 0 0 0 24 12Z" />
    </svg>
  ),
};

// Brand colour each social button tints to on hover.
const SOCIAL_BRAND_HOVER: Record<string, string> = {
  Instagram: "hover:text-[#E4405F] hover:border-[#E4405F]/40",
  YouTube: "hover:text-[#FF0000] hover:border-[#FF0000]/40",
  TikTok: "hover:text-white hover:border-white/40",
  X: "hover:text-white hover:border-white/40",
  Facebook: "hover:text-[#1877F2] hover:border-[#1877F2]/40",
};

function MailIcon(props: IconProps) {
  return (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden {...props}>
      <rect x="3" y="5" width="18" height="14" rx="2" />
      <path d="m3 7 9 6 9-6" />
    </svg>
  );
}

function PhoneIcon(props: IconProps) {
  return (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" aria-hidden {...props}>
      <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.8 19.8 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72c.13.96.36 1.9.7 2.81a2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45c.9.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92Z" />
    </svg>
  );
}

function WhatsAppIcon(props: IconProps) {
  return (
    <svg viewBox="0 0 24 24" fill="currentColor" aria-hidden {...props}>
      <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.885-9.885 9.885M20.52 3.449C18.24 1.245 15.24 0 12.045 0 5.463 0 .104 5.334.101 11.892c0 2.096.549 4.14 1.595 5.945L0 24l6.335-1.652a12.062 12.062 0 005.71 1.447h.005c6.585 0 11.946-5.335 11.949-11.893a11.821 11.821 0 00-3.479-8.413" />
    </svg>
  );
}

export default async function SiteFooter() {
  const phoneTel = CONTACT.phone.replace(/\s/g, "");
  const whatsappNumber = CONTACT.phone.replace(/[^\d]/g, "");
  const socials = await getSocialLinks();

  return (
    <footer id="about" className="bg-panel border-t border-line text-ink">
      <div className="max-w-6xl mx-auto px-4 py-12 grid sm:grid-cols-3 gap-8">
        <div className="sm:col-span-1">
          <BrandLogo size="md" showTagline />
          <p className="mt-3 text-sm font-semibold text-ink/90">{PLATFORM.tagline}</p>
          <p className="mt-2 text-sm text-muted max-w-xs">
            VibePass Tickets powers concerts, festivals, gaming, wellness and nightlife
            events across Kenya — instant M-Pesa checkout for buyers, the lowest fees
            and same-day payouts for organizers.
          </p>
          <div className="mt-5 flex flex-wrap gap-2.5">
            {socials.map((s) => {
              const Icon = SocialIcons[s.label];
              return (
                <a
                  key={s.label}
                  href={s.url}
                  target="_blank"
                  rel="noopener noreferrer"
                  aria-label={s.label}
                  title={s.handle}
                  className={`grid h-10 w-10 place-items-center rounded-full border border-line text-muted transition-colors ${
                    SOCIAL_BRAND_HOVER[s.label] ?? "hover:text-white hover:border-white/40"
                  }`}
                >
                  {Icon ? <Icon className="h-[18px] w-[18px]" /> : <span className="text-xs">{s.label}</span>}
                </a>
              );
            })}
          </div>
        </div>
        <div>
          <p className="text-xs uppercase tracking-widest text-muted mb-3">Tickets</p>
          <ul className="space-y-2 text-sm text-ink/80">
            <li>
              <Link href="/" className="hover:text-white">
                Get tickets
              </Link>
            </li>
            <li>
              <Link href="/events" className="hover:text-white">
                All events
              </Link>
            </li>
            <li>
              <Link href="/shop" className="hover:text-white">
                Merch shop
              </Link>
            </li>
            <li>
              <Link href="/bash" className="hover:text-white">
                Bash — hire &amp; production
              </Link>
            </li>
            <li>
              <Link href="/gallery" className="hover:text-white">
                Past events gallery
              </Link>
            </li>
            <li>
              <Link href="/dashboard" className="hover:text-white">
                Organizer login
              </Link>
            </li>
          </ul>
        </div>
        <div>
          <p className="text-xs uppercase tracking-widest text-muted mb-3">Contact</p>
          <ul className="space-y-3 text-sm text-ink/80">
            <li>
              <a href={`mailto:${CONTACT.email}`} className="group flex items-center gap-3 hover:text-white">
                <span className="grid h-8 w-8 shrink-0 place-items-center rounded-full bg-white/5 text-muted transition-colors group-hover:bg-brand-green group-hover:text-white">
                  <MailIcon className="h-4 w-4" />
                </span>
                {CONTACT.email}
              </a>
            </li>
            <li>
              <a href={`tel:${phoneTel}`} className="group flex items-center gap-3 hover:text-white">
                <span className="grid h-8 w-8 shrink-0 place-items-center rounded-full bg-white/5 text-muted transition-colors group-hover:bg-brand-green group-hover:text-white">
                  <PhoneIcon className="h-4 w-4" />
                </span>
                {CONTACT.phone}
              </a>
            </li>
            <li>
              <a
                href={`https://wa.me/${whatsappNumber}`}
                target="_blank"
                rel="noopener noreferrer"
                className="group flex items-center gap-3 hover:text-white"
              >
                <span className="grid h-8 w-8 shrink-0 place-items-center rounded-full bg-white/5 text-muted transition-colors group-hover:bg-[#25D366] group-hover:text-white">
                  <WhatsAppIcon className="h-4 w-4" />
                </span>
                WhatsApp
              </a>
            </li>
          </ul>
        </div>
      </div>
      <div className="border-t border-line">
        <div className="max-w-6xl mx-auto px-4 py-5 flex flex-wrap items-center justify-between gap-3 text-xs text-muted">
          <span>© 2026 VibePass Digital, Nairobi.</span>
          <span className="flex flex-wrap items-center gap-x-4 gap-y-1">
            <Link href="/terms" className="hover:text-white">
              Terms of Service
            </Link>
            <Link href="/privacy" className="hover:text-white">
              Privacy Policy
            </Link>
            <Link href="/data-deletion" className="hover:text-white">
              Data Deletion
            </Link>
            <span>
              Produced by{" "}
              <a
                href={`https://${CONTACT.website}`}
                target="_blank"
                rel="noopener noreferrer"
                className="font-semibold text-white hover:text-brand-green-h"
              >
                VibePass
              </a>
            </span>
          </span>
          <span className="rounded-full bg-white/10 px-3 py-1 font-medium text-ink/80">
            Pay via M-Pesa
          </span>
        </div>
      </div>
      {/* Reserves room at the true bottom of the page for the fixed
          WhatsApp/back-to-top buttons (FloatingActions, bottom-5 right-5 in
          viewport space). They don't take up document flow space, so without
          this the footer's last line sits directly under them once the page
          is scrolled to the end — a plain spacer is more reliable than
          padding on the row above, since that row's own height changes
          depending on whether it wraps to one line or two. */}
      <div aria-hidden className="h-32 bg-panel" />
    </footer>
  );
}
