import React, { useState } from "react";
import { Link, useLocation } from "react-router-dom";
import { primaryNav, footerNavGroups } from "../data/navLinks";
import { Bars3Icon, XMarkIcon } from "@heroicons/react/24/outline";

interface MarketingLayoutProps {
  children: React.ReactNode;
}

const MarketingLayout: React.FC<MarketingLayoutProps> = ({ children }) => {
  const [mobileOpen, setMobileOpen] = useState(false);
  const location = useLocation();

  const toggleMobile = () => setMobileOpen((prev) => !prev);
  const closeMobile = () => setMobileOpen(false);

  return (
    <div className="min-h-screen flex flex-col bg-gradient-to-b from-white via-slate-50 to-white text-slate-900">
      <header className="sticky top-0 z-[1100] backdrop-blur-xl bg-white/95 border-b border-slate-200 shadow-sm">
        <div className="max-w-6xl mx-auto px-6 py-4 flex items-center justify-between">
          <Link to="/" className="flex items-center gap-3" onClick={closeMobile}>
            <span className="h-10 w-10 rounded-xl bg-gradient-to-br from-[var(--color-primary)] to-[var(--color-primary-dark)] flex items-center justify-center text-lg font-bold text-white shadow-lg shadow-[var(--color-primary)]/30">
              IG
            </span>
            <div className="flex flex-col leading-tight">
              <span className="font-semibold tracking-wide text-slate-900 uppercase">
                Igny8
              </span>
              <span className="text-xs text-slate-600">AI growth engine</span>
            </div>
          </Link>

          <nav className="hidden lg:flex items-center gap-8 text-sm font-medium">
            {primaryNav.map((link) => {
              const isActive = location.pathname === link.path;
              return (
                <Link
                  key={link.name}
                  to={link.path}
                  className={`transition hover:text-[var(--color-primary)] ${isActive ? "text-[var(--color-primary)] font-semibold" : "text-slate-700"}`}
                >
                  {link.name}
                </Link>
              );
            })}
          </nav>

          <div className="hidden lg:flex items-center gap-4">
            <a
              href="https://app.igny8.com/login"
              className="text-sm font-medium text-slate-700 hover:text-slate-900 transition"
            >
              Log in
            </a>
            <a
              href="https://app.igny8.com/signup"
              className="inline-flex items-center justify-center rounded-full bg-gradient-to-r from-[var(--color-primary)] to-[var(--color-primary-dark)] hover:from-[var(--color-primary-dark)] hover:to-[var(--color-primary)] text-white px-5 py-2 text-sm font-semibold transition shadow-md shadow-[var(--color-primary)]/30"
            >
              Start Free
            </a>
          </div>

          <button
            className="lg:hidden inline-flex items-center justify-center rounded-lg border border-slate-200 p-2 text-slate-700"
            onClick={toggleMobile}
            aria-label="Toggle navigation"
          >
            {mobileOpen ? <XMarkIcon className="h-6 w-6" /> : <Bars3Icon className="h-6 w-6" />}
          </button>
        </div>

        {mobileOpen && (
          <div className="lg:hidden border-t border-slate-200 bg-white backdrop-blur-xl">
            <nav className="px-6 py-4 flex flex-col gap-3">
              {primaryNav.map((link) => (
                <Link
                  key={link.name}
                  to={link.path}
                  onClick={closeMobile}
                  className="text-sm font-medium text-slate-700 hover:text-slate-900 transition"
                >
                  {link.name}
                </Link>
              ))}
            </nav>
            <div className="px-6 pb-6 flex flex-col gap-3">
              <a
                href="https://app.igny8.com/login"
                className="text-sm font-medium text-slate-700 hover:text-slate-900 transition"
                onClick={closeMobile}
              >
                Log in
              </a>
              <a
                href="https://app.igny8.com/signup"
                className="inline-flex items-center justify-center rounded-full bg-gradient-to-r from-[var(--color-primary)] to-[var(--color-primary-dark)] hover:from-[var(--color-primary-dark)] hover:to-[var(--color-primary)] text-white px-5 py-2 text-sm font-semibold transition shadow-md shadow-[var(--color-primary)]/30"
                onClick={closeMobile}
              >
                Start Free
              </a>
            </div>
          </div>
        )}
      </header>

      <main className="flex-1">{children}</main>

      <footer className="bg-gradient-to-br from-[#0d1b2a] via-[#142b3f] to-[#0d1b2a] border-t border-[#0693e3]/20">
        <div className="max-w-6xl mx-auto px-6 py-16 grid grid-cols-1 md:grid-cols-4 gap-12">
          <div className="space-y-4">
            <Link to="/" className="inline-flex items-center gap-3">
              <span className="h-10 w-10 rounded-xl bg-gradient-to-br from-[var(--color-primary)] to-[var(--color-primary-dark)] flex items-center justify-center text-lg font-bold text-white shadow-lg shadow-[var(--color-primary)]/30">
                IG
              </span>
              <div className="flex flex-col leading-tight">
                <span className="font-semibold tracking-wide text-white uppercase">
                  Igny8
                </span>
                <span className="text-xs text-slate-300">
                  AI + SEO automation suite
                </span>
              </div>
            </Link>
            <p className="text-sm text-slate-300 max-w-xs">
              Automate keyword intelligence, clustering, content production, and image creation in one unified growth engine.
            </p>
            <div className="text-xs text-slate-400">
              © {new Date().getFullYear()} Igny8 Labs. All rights reserved.
            </div>
          </div>

          {footerNavGroups.map((group) => (
            <div key={group.title}>
              <h4 className="text-sm font-semibold text-white uppercase tracking-wide mb-4">
                {group.title}
              </h4>
              <ul className="space-y-3 text-sm text-slate-300">
                {group.links.map((link) => (
                  <li key={link.name}>
                    <Link to={link.path} className="hover:text-[var(--color-primary)] transition">
                      {link.name}
                    </Link>
                  </li>
                ))}
              </ul>
            </div>
          ))}
        </div>
        <div className="border-t border-[var(--color-primary)]/20 py-6 px-6 text-center text-xs text-slate-400">
          Built for marketers who automate growth with AI.
        </div>
      </footer>
    </div>
  );
};

export default MarketingLayout;

