import Link from "next/link";
import { db } from "@/lib/db";
import { Mail, MapPin, Phone } from "lucide-react";

type LinkItem = { label: string; url: string };
type SocialItem = { platform: string; url: string; icon: string };

const BrandIcons: Record<string, React.ReactNode> = {
  facebook: <svg fill="currentColor" viewBox="0 0 24 24" className="w-full h-full"><path d="M22 12c0-5.523-4.477-10-10-10S2 6.477 2 12c0 4.991 3.657 9.128 8.438 9.878v-6.987h-2.54V12h2.54V9.797c0-2.506 1.492-3.89 3.777-3.89 1.094 0 2.238.195 2.238.195v2.46h-1.26c-1.243 0-1.63.771-1.63 1.562V12h2.773l-.443 2.89h-2.33v6.988C18.343 21.128 22 16.991 22 12z"/></svg>,
  instagram: <svg fill="currentColor" viewBox="0 0 24 24" className="w-full h-full"><path fillRule="evenodd" d="M12.315 2c2.43 0 2.784.013 3.808.06 1.064.049 1.791.218 2.427.465a4.902 4.902 0 011.772 1.153 4.902 4.902 0 011.153 1.772c.247.636.416 1.363.465 2.427.048 1.067.06 1.407.06 4.123v.08c0 2.643-.012 2.987-.06 4.043-.049 1.064-.218 1.791-.465 2.427a4.902 4.902 0 01-1.153 1.772 4.902 4.902 0 01-1.772 1.153c-.636.247-1.363.416-2.427.465-1.067.048-1.407.06-4.123.06h-.08c-2.643 0-2.987-.012-4.043-.06-1.064-.049-1.791-.218-2.427-.465a4.902 4.902 0 01-1.772-1.153 4.902 4.902 0 01-1.153-1.772c-.247-.636-.416-1.363-.465-2.427-.047-1.024-.06-1.379-.06-3.808v-.63c0-2.43.013-2.784.06-3.808.049-1.064.218-1.791.465-2.427a4.902 4.902 0 011.153-1.772A4.902 4.902 0 015.45 2.525c.636-.247 1.363-.416 2.427-.465C8.901 2.013 9.256 2 11.685 2h.63zm-.081 1.802h-.468c-2.456 0-2.784.011-3.807.058-.975.045-1.504.207-1.857.344-.467.182-.8.398-1.15.748-.35.35-.566.683-.748 1.15-.137.353-.3.882-.344 1.857-.047 1.023-.058 1.351-.058 3.807v.468c0 2.456.011 2.784.058 3.807.045.975.207 1.504.344 1.857.182.466.399.8.748 1.15.35.35.683.566 1.15.748.353.137.882.3 1.857.344 1.054.048 1.37.058 4.041.058h.08c2.597 0 2.917-.01 3.96-.058.976-.045 1.505-.207 1.858-.344.466-.182.8-.398 1.15-.748.35-.35.566-.683.748-1.15.137-.353.3-.882.344-1.857.048-1.055.058-1.37.058-4.041v-.08c0-2.597-.01-2.917-.058-3.96-.045-.976-.207-1.505-.344-1.858a3.097 3.097 0 00-.748-1.15 3.098 3.098 0 00-1.15-.748c-.353-.137-.882-.3-1.857-.344-1.023-.047-1.351-.058-3.807-.058zM12 6.865a5.135 5.135 0 110 10.27 5.135 5.135 0 010-10.27zm0 1.802a3.333 3.333 0 100 6.666 3.333 3.333 0 000-6.666zm5.338-3.205a1.2 1.2 0 110 2.4 1.2 1.2 0 010-2.4z" clipRule="evenodd"/></svg>,
  linkedin: <svg fill="currentColor" viewBox="0 0 24 24" className="w-full h-full"><path d="M19 0h-14c-2.761 0-5 2.239-5 5v14c0 2.761 2.239 5 5 5h14c2.762 0 5-2.239 5-5v-14c0-2.761-2.238-5-5-5zm-11 19h-3v-11h3v11zm-1.5-12.268c-.966 0-1.75-.79-1.75-1.764s.784-1.764 1.75-1.764 1.75.79 1.75 1.764-.783 1.764-1.75 1.764zm13.5 12.268h-3v-5.604c0-3.368-4-3.113-4 0v5.604h-3v-11h3v1.765c1.396-2.586 7-2.777 7 2.476v6.759z"/></svg>
};

export default async function Footer() {
  // If not configured in DB yet, use hardcoded high-end defaults
  let settings: any = {
      id: "singleton",
      description: "RIMA Academy is an elite online learning platform offering masterclasses in languages, beauty, business management, and marketing.",
      col1Title: "Explore",
      col1Links: JSON.stringify([
        { label: "Our Courses", url: "/courses" },
        { label: "About Academy", url: "/about" },
        { label: "Corporate Training", url: "/courses?type=corporate" },
        { label: "Student Dashboard", url: "/dashboard" }
      ]),
      col2Title: "Legal & Support",
      col2Links: JSON.stringify([
        { label: "Terms of Service", url: "/terms" },
        { label: "Privacy Policy", url: "/privacy" },
        { label: "Refund Policy", url: "/refunds" },
        { label: "Help Center", url: "/support" }
      ]),
      col3Title: "Contact Us",
      col3Links: JSON.stringify([
        { label: "contact@rima-academy.com", url: "mailto:contact@rima-academy.com" },
        { label: "+40 700 000 000", url: "tel:+40700000000" },
        { label: "Bucharest, Romania", url: "#" }
      ]),
      socialLinks: JSON.stringify([
        { platform: "Facebook", url: "#", icon: "facebook" },
        { platform: "Instagram", url: "#", icon: "instagram" },
        { platform: "LinkedIn", url: "#", icon: "linkedin" }
      ]),
      updatedAt: new Date()
    };

  // Parse JSON safely
  const col1 = JSON.parse(settings.col1Links) as LinkItem[];
  const col2 = JSON.parse(settings.col2Links) as LinkItem[];
  const col3 = JSON.parse(settings.col3Links) as LinkItem[];
  const socials = JSON.parse(settings.socialLinks) as SocialItem[];

  return (
    <footer className="relative bg-[#020306] border-t border-white/5 pt-20 pb-10 overflow-hidden font-sans text-slate-400">
      
      {/* ── Ambient Background Meshes ── */}
      <div className="absolute inset-0 pointer-events-none z-0">
        <div className="absolute top-[-20%] right-[10%] w-[40%] h-[40%] bg-[#dc2626]/10 rounded-full blur-[120px]" />
        <div className="absolute bottom-[-10%] left-[-10%] w-[40%] h-[40%] bg-[#102d56]/10 rounded-full blur-[120px]" />
        <div className="absolute inset-0 opacity-[0.01]" style={{ backgroundImage: "linear-gradient(rgba(255,255,255,0.1) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.1) 1px,transparent 1px)", backgroundSize: "40px 40px" }} />
      </div>

      <div className="max-w-7xl mx-auto px-6 relative z-10">
        <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-12 gap-12 lg:gap-8 mb-16">
          
          {/* Column 1: Branding (Spans 4 columns) */}
          <div className="lg:col-span-4">
            <Link href="/" className="inline-flex items-center gap-3 group mb-6">
              <div className="w-10 h-10 bg-white/5 rounded-xl flex items-center justify-center border border-white/10 group-hover:border-cyan-500/50 transition-colors">
                <img src="/logo-2.png" alt="Rima Logo" className="w-6 h-6 object-contain" />
              </div>
              <span className="font-black text-xl tracking-wide text-white group-hover:text-cyan-400 transition-colors">RIMA ACADEMY</span>
            </Link>
            <p className="text-sm leading-relaxed mb-8 max-w-sm">
              {settings.description}
            </p>
            <div className="flex items-center gap-4">
              {socials.map((social, idx) => {
                return (
                  <a 
                    key={idx} 
                    href={social.url} 
                    target="_blank" 
                    rel="noreferrer"
                    aria-label={social.platform}
                    className="w-10 h-10 rounded-full bg-white/5 flex items-center justify-center hover:bg-white/10 hover:text-white transition-all border border-white/5 hover:border-white/20 p-2.5"
                  >
                    {BrandIcons[social.icon.toLowerCase()] || <div className="w-full h-full bg-white/50 rounded-full" />}
                  </a>
                );
              })}
            </div>
          </div>

          {/* Column 2: Explore */}
          <div className="lg:col-span-2 lg:col-start-6">
            <h4 className="text-white font-bold mb-6 tracking-wide">{settings.col1Title}</h4>
            <ul className="space-y-4">
              {col1.map((link, idx) => (
                <li key={idx}>
                  <Link href={link.url} className="text-sm hover:text-cyan-400 transition-colors flex items-center gap-2 group">
                    <span className="w-0 h-0.5 bg-cyan-400 transition-all duration-300 group-hover:w-2" />
                    {link.label}
                  </Link>
                </li>
              ))}
            </ul>
          </div>

          {/* Column 3: Legal */}
          <div className="lg:col-span-2">
            <h4 className="text-white font-bold mb-6 tracking-wide">{settings.col2Title}</h4>
            <ul className="space-y-4">
              {col2.map((link, idx) => (
                <li key={idx}>
                  <Link href={link.url} className="text-sm hover:text-cyan-400 transition-colors flex items-center gap-2 group">
                    <span className="w-0 h-0.5 bg-cyan-400 transition-all duration-300 group-hover:w-2" />
                    {link.label}
                  </Link>
                </li>
              ))}
            </ul>
          </div>

          {/* Column 4: Contact */}
          <div className="lg:col-span-3 lg:col-start-10">
            <h4 className="text-white font-bold mb-6 tracking-wide">{settings.col3Title}</h4>
            <ul className="space-y-5">
              {col3.map((link, idx) => {
                // Determine icon based on content heuristics
                const isEmail = link.label.includes('@');
                const isPhone = link.label.includes('+') || /\d{4}/.test(link.label);
                return (
                  <li key={idx}>
                    <a href={link.url} className="text-sm hover:text-white transition-colors flex items-start gap-3 group">
                      <div className="mt-0.5 text-slate-500 group-hover:text-cyan-400 transition-colors">
                        {isEmail ? <Mail className="w-4 h-4" /> : isPhone ? <Phone className="w-4 h-4" /> : <MapPin className="w-4 h-4" />}
                      </div>
                      <span className="flex-1">{link.label}</span>
                    </a>
                  </li>
                );
              })}
            </ul>
          </div>
          
        </div>

        {/* Bottom Bar */}
        <div className="pt-8 border-t border-white/5 flex flex-col md:flex-row items-center justify-between gap-4 text-xs">
          <p>© {new Date().getFullYear()} RIMA Academy. All rights reserved.</p>
          <div className="flex items-center gap-1 font-medium">
            Designed with <span className="text-[#dc2626] animate-pulse">❤</span> for premium education.
          </div>
        </div>
      </div>
    </footer>
  );
}
