"use client";

import { useState, useEffect } from "react";
import Link from "next/link";
import GoogleTranslateFlags from "./GoogleTranslateFlags";

type User = {
  id: string;
  name?: string | null;
  email?: string | null;
  role?: string;
};

export default function HomeHeader({ user }: { user: User | null }) {
  const [isVisible, setIsVisible] = useState(true);
  const [lastScrollY, setLastScrollY] = useState(0);

  useEffect(() => {
    const handleScroll = () => {
      if (typeof window !== "undefined") {
        if (window.scrollY > lastScrollY && window.scrollY > 100) {
          setIsVisible(false);
        } else {
          setIsVisible(true);
        }
        setLastScrollY(window.scrollY);
      }
    };
    window.addEventListener("scroll", handleScroll);
    return () => window.removeEventListener("scroll", handleScroll);
  }, [lastScrollY]);

  return (
    <nav className={`fixed w-full z-50 bg-white/95 backdrop-blur-xl border-b border-slate-200 transition-transform duration-300 ${isVisible ? 'translate-y-0' : '-translate-y-full'}`}>
      <div className="w-full px-6 md:px-12 lg:px-24 h-20 flex items-center justify-between">
        {/* Improved Logo */}
        <Link href="/" className="flex items-center gap-4 group">
          <div className="relative flex items-center justify-center w-12 h-12">
            {/* The customized R */}
            <svg viewBox="0 0 100 100" className="w-full h-full drop-shadow-md">
              <defs>
                <linearGradient id="globeGrad" x1="0%" y1="0%" x2="100%" y2="100%">
                  <stop offset="0%" stopColor="#1E3A8A" />
                  <stop offset="100%" stopColor="#312E81" />
                </linearGradient>
                <linearGradient id="bookGrad" x1="0%" y1="0%" x2="100%" y2="100%">
                  <stop offset="0%" stopColor="#991B1B" />
                  <stop offset="100%" stopColor="#7F1D1D" />
                </linearGradient>
              </defs>
              <path d="M20,20 L45,20 C60,20 65,30 65,40 C65,48 58,55 48,58 L68,85 L45,85 L30,60 L30,85 L20,85 Z" fill="#1E3A5F" />
              <circle cx="45" cy="35" r="12" fill="url(#globeGrad)" />
              <path d="M40,30 Q45,25 50,30 Q55,35 50,40 Q45,45 40,40 Z" fill="#E0E7FF" opacity="0.6"/>
              <path d="M15,75 Q30,65 45,75 Q30,85 15,75 Z" fill="url(#bookGrad)" />
              <path d="M45,75 Q60,65 75,75 Q60,85 45,75 Z" fill="url(#bookGrad)" />
            </svg>
          </div>
          
          <div className="flex flex-col">
            <div className="flex items-baseline gap-1">
              <span className="font-serif text-2xl font-black tracking-widest text-[#1E3A5F]">RIMA</span>
            </div>
            <div className="flex items-center gap-2">
              <div className="h-[1px] w-6 bg-[#991B1B]"></div>
              <span className="font-serif text-[10px] tracking-[0.3em] text-[#991B1B] font-bold">ACADEMY</span>
              <div className="h-[1px] w-6 bg-[#991B1B]"></div>
            </div>
          </div>
          
          <div className="hidden lg:flex flex-col ml-4 pl-4 border-l border-slate-300 justify-center">
            <span className="text-[9px] font-black tracking-widest text-[#4A1D5E] leading-tight">LEARN <br/>SPEAK</span>
            <span className="text-[9px] font-black tracking-widest text-[#4A1D5E] leading-tight">CONNECT <br/>SUCCEED</span>
          </div>
        </Link>
        
        <div className="hidden md:flex items-center gap-8 text-sm font-bold">
          <Link href="/" className="text-slate-600 hover:text-indigo-900 transition-colors">Home</Link>
          <Link href="/courses" className="text-slate-600 hover:text-indigo-900 transition-colors">Our Courses</Link>
          <Link href="/about" className="text-slate-600 hover:text-indigo-900 transition-colors">About Me</Link>
          <Link href="/contact" className="text-slate-600 hover:text-indigo-900 transition-colors">Contact</Link>
        </div>

        <div className="flex items-center gap-4">
          <GoogleTranslateFlags />

          {user ? (
            <Link 
              href={user.role === 'ADMIN' || user.role === 'SUPER_ADMIN' ? "/admin" : "/dashboard"} 
              className="bg-[#1E3A5F] hover:bg-[#1E3A5F]/90 text-white text-sm font-semibold px-5 py-2.5 rounded-[12px] shadow-lg shadow-[#1E3A5F]/20 transition-all"
            >
              {user.role === 'ADMIN' || user.role === 'SUPER_ADMIN' ? "Admin Dashboard" : "Dashboard"}
            </Link>
          ) : (
            <>
              <Link href="/login" className="text-sm font-bold text-slate-700 hover:text-[#991B1B] transition-colors hidden sm:block">
                Login
              </Link>
              <Link href="/register" className="bg-[#991B1B] hover:bg-[#7F1D1D] text-white text-sm font-bold px-6 py-2.5 rounded-[12px] shadow-lg shadow-[#991B1B]/30 transition-all">
                Register
              </Link>
            </>
          )}
        </div>
      </div>
    </nav>
  );
}
