"use client";

import { useEffect, useState } from "react";
import { motion } from "framer-motion";
import { Menu, PenLine, ShieldCheck } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
  Sheet,
  SheetContent,
  SheetHeader,
  SheetTitle,
  SheetTrigger,
} from "@/components/ui/sheet";
import Logo from "@/components/ppdb/Logo";
import { navigate } from "@/lib/router";

const LINKS = [
  { id: "beranda", label: "Beranda" },
  { id: "jurusan", label: "Jurusan" },
  { id: "alur", label: "Alur" },
  { id: "persyaratan", label: "Persyaratan" },
  { id: "faq", label: "FAQ" },
  { id: "kontak", label: "Kontak" },
];

export function scrollToId(id: string) {
  const el = document.getElementById(id);
  if (el) el.scrollIntoView({ behavior: "smooth", block: "start" });
}

export default function Navbar({ schoolName }: { schoolName?: string }) {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 12);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const go = (id: string) => {
    setOpen(false);
    navigate("");
    setTimeout(() => scrollToId(id), 80);
  };

  return (
    <motion.header
      initial={{ y: -70, opacity: 0 }}
      animate={{ y: 0, opacity: 1 }}
      transition={{ duration: 0.45, ease: "easeOut" }}
      className={`fixed top-0 inset-x-0 z-50 glass-card border-b transition-all duration-300 ${
        scrolled ? "shadow-md shadow-[#7B1E28]/10 border-[#7B1E28]/15" : "border-white/10"
      }`}
    >
      <nav className="mx-auto flex h-16 max-w-7xl items-center justify-between gap-3 px-4 sm:px-6" aria-label="Navigasi utama">
        <button onClick={() => go("beranda")} className="flex items-center gap-2.5 min-w-0 group">
          <Logo size={38} className="shrink-0 transition-transform duration-300 group-hover:rotate-6 group-hover:scale-105" />
          <span className="flex flex-col items-start leading-tight min-w-0">
            <span className="truncate text-sm sm:text-base font-extrabold tracking-tight text-[#4a1520]">
              SMK {schoolName || "Al Aitaam"}
            </span>
            <span className="text-[10px] sm:text-[11px] font-semibold uppercase tracking-widest text-[#8a6a10]">
              PPDB Online
            </span>
          </span>
        </button>

        <div className="hidden lg:flex items-center gap-1">
          {LINKS.map((l) => (
            <button
              key={l.id}
              onClick={() => go(l.id)}
              className="rounded-full px-3.5 py-2 text-sm font-medium text-[#4a1520]/80 transition-colors hover:bg-[#7B1E28]/8 hover:text-[#7B1E28]"
            >
              {l.label}
            </button>
          ))}
        </div>

        <div className="flex items-center gap-2">
          <Button
            variant="ghost"
            size="sm"
            onClick={() => navigate("admin")}
            className="hidden sm:inline-flex gap-1.5 text-[#4a1520]/70 hover:text-[#7B1E28]"
          >
            <ShieldCheck className="size-4" />
            Panitia
          </Button>
          <Button
            size="sm"
            onClick={() => navigate("daftar")}
            className="gap-1.5 rounded-full bg-gradient-to-r from-[#7B1E28] to-[#a32838] text-white shadow-lg shadow-[#7B1E28]/25 hover:shadow-xl hover:shadow-[#7B1E28]/35 transition-all hover:-translate-y-0.5"
          >
            <PenLine className="size-4" />
            Daftar
          </Button>
          <Sheet open={open} onOpenChange={setOpen}>
            <SheetTrigger asChild>
              <Button variant="outline" size="icon" className="lg:hidden rounded-full" aria-label="Buka menu">
                <Menu className="size-5" />
              </Button>
            </SheetTrigger>
            <SheetContent side="right" className="w-72">
              <SheetHeader>
                <SheetTitle className="flex items-center gap-2">
                  <Logo size={30} /> SMK {schoolName || "Al Aitaam"}
                </SheetTitle>
              </SheetHeader>
              <div className="mt-2 flex flex-col gap-1 px-2">
                {LINKS.map((l) => (
                  <button
                    key={l.id}
                    onClick={() => go(l.id)}
                    className="rounded-xl px-4 py-3 text-left text-sm font-medium transition-colors hover:bg-[#7B1E28]/8 hover:text-[#7B1E28]"
                  >
                    {l.label}
                  </button>
                ))}
                <button
                  onClick={() => {
                    setOpen(false);
                    navigate("admin");
                  }}
                  className="mt-2 flex items-center gap-2 rounded-xl px-4 py-3 text-left text-sm font-medium text-[#7B1E28] border border-[#7B1E28]/20 hover:bg-[#7B1E28]/8"
                >
                  <ShieldCheck className="size-4" /> Masuk Panitia
                </button>
              </div>
            </SheetContent>
          </Sheet>
        </div>
      </nav>
    </motion.header>
  );
}
