import React, { useState, useEffect } from 'react';
import { PageType } from '../types';
import { Menu, X, Calendar, PhoneCall } from 'lucide-react';
import { PRACTICE_INFO } from '../data/mockData';
import logoImage from '../assets/images/regenerated_image_1783412405039.jpg';

interface NavbarProps {
  currentPage: PageType;
  onNavigate: (page: PageType) => void;
  onOpenBooking: () => void;
  onOpenContactModal: () => void;
}

export const Navbar: React.FC<NavbarProps> = ({
  currentPage,
  onNavigate,
  onOpenBooking,
  onOpenContactModal
}) => {
  const [isScrolled, setIsScrolled] = useState(false);
  const [mobileMenuOpen, setMobileMenuOpen] = useState(false);

  useEffect(() => {
    const handleScroll = () => {
      if (window.scrollY > 20) {
        setIsScrolled(true);
      } else {
        setIsScrolled(false);
      }
    };
    window.addEventListener('scroll', handleScroll);
    return () => window.removeEventListener('scroll', handleScroll);
  }, []);

  const navItems: { label: string; id: PageType }[] = [
    { label: 'Home', id: 'home' },
    { label: 'About Us', id: 'about' },
    { label: 'Services', id: 'services' },
  ];

  const handleNavClick = (page: PageType) => {
    onNavigate(page);
    setMobileMenuOpen(false);
    window.scrollTo({ top: 0, behavior: 'smooth' });
  };

  return (
    <header
      className={`sticky top-0 z-40 transition-all duration-300 ${
        isScrolled
          ? 'bg-white/95 backdrop-blur-md shadow-md py-3 border-b border-slate-100'
          : 'bg-white py-4 border-b border-slate-100'
      }`}
    >
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div className="flex items-center justify-between">
          {/* Logo */}
          <button
            onClick={() => handleNavClick('home')}
            className="flex items-center space-x-3 text-left focus:outline-none group"
          >
            <div className="relative">
              <div className="w-11 h-11 sm:w-12 sm:h-12 rounded-xl bg-white border border-slate-200/80 flex items-center justify-center overflow-hidden shadow-xs group-hover:scale-105 group-hover:border-[#0A6EB4] transition-all duration-300 p-0.5">
                <img 
                  src={logoImage} 
                  alt="Dr. Matseke Surgery Logo" 
                  className="w-full h-full object-contain"
                  referrerPolicy="no-referrer"
                />
              </div>
              <span className="absolute -top-1 -right-1 w-3.5 h-3.5 bg-[#E63946] border-2 border-white rounded-full flex items-center justify-center shadow-2xs z-10" title="Emergency On-Call Ready">
                <span className="w-1.5 h-1.5 bg-white rounded-full animate-pulse"></span>
              </span>
            </div>
            <div>
              <span className="block text-lg sm:text-xl font-bold tracking-tight text-slate-900 group-hover:text-[#0A6EB4] transition-colors">
                Dr. Matseke <span className="text-[#0A6EB4] font-extrabold">Surgery</span>
              </span>
              <span className="block text-[11px] font-medium uppercase tracking-wider text-[#2FA8A1]">
                Family & Chronic Medical Care
              </span>
            </div>
          </button>

          {/* Desktop Navigation Links */}
          <nav className="hidden md:flex items-center space-x-1 lg:space-x-2">
            {navItems.map((item) => {
              const isActive = currentPage === item.id;
              return (
                <button
                  key={item.id}
                  onClick={() => handleNavClick(item.id)}
                  className={`px-4 py-2 rounded-lg font-medium text-sm transition-all relative ${
                    isActive
                      ? 'text-primary font-semibold bg-medical-accent/70'
                      : 'text-slate-600 hover:text-primary hover:bg-slate-50'
                  }`}
                >
                  {item.label}
                  {isActive && (
                    <span className="absolute bottom-0 left-3 right-3 h-0.5 bg-primary rounded-full" />
                  )}
                </button>
              );
            })}
          </nav>

          {/* Right Action Buttons */}
          <div className="hidden md:flex items-center space-x-3">
            <button
              onClick={onOpenContactModal}
              className="px-5 py-2.5 rounded-full text-slate-700 font-bold text-sm hover:bg-slate-100 border border-slate-200 transition-all flex items-center space-x-1.5"
            >
              <PhoneCall className="w-4 h-4 text-[#0A6EB4]" />
              <span>Contact Us</span>
            </button>

            <button
              onClick={onOpenBooking}
              className="bg-[#2FA8A1] hover:bg-[#28928b] text-white px-6 py-2.5 rounded-full font-bold text-sm shadow-md hover:shadow-lg transition-all flex items-center space-x-2 active:scale-95"
            >
              <Calendar className="w-4 h-4" />
              <span>Book Appointment</span>
            </button>
          </div>

          {/* Mobile Hamburger Button */}
          <div className="flex items-center space-x-2 md:hidden">
            <button
              onClick={onOpenBooking}
              className="bg-[#2FA8A1] text-white px-3 py-2 rounded-full text-xs font-bold flex items-center space-x-1 shadow-sm"
              aria-label="Book appointment"
            >
              <Calendar className="w-4 h-4" />
              <span>Book</span>
            </button>
            <button
              onClick={() => setMobileMenuOpen(!mobileMenuOpen)}
              className="p-2.5 rounded-xl text-slate-700 hover:text-primary hover:bg-slate-100 focus:outline-none transition-colors"
              aria-label="Toggle navigation menu"
            >
              {mobileMenuOpen ? <X className="w-6 h-6" /> : <Menu className="w-6 h-6" />}
            </button>
          </div>
        </div>
      </div>

      {/* Mobile Menu Dropdown */}
      {mobileMenuOpen && (
        <div className="md:hidden bg-white border-t border-slate-100 px-4 pt-3 pb-6 space-y-3 shadow-xl animate-in slide-in-from-top duration-200">
          <div className="space-y-1">
            {navItems.map((item) => (
              <button
                key={item.id}
                onClick={() => handleNavClick(item.id)}
                className={`w-full text-left px-4 py-3 rounded-xl font-medium text-base transition-colors flex items-center justify-between ${
                  currentPage === item.id
                    ? 'bg-primary/10 text-primary font-bold'
                    : 'text-slate-700 hover:bg-slate-50'
                }`}
              >
                <span>{item.label}</span>
                {currentPage === item.id && <span className="w-2 h-2 rounded-full bg-primary" />}
              </button>
            ))}
          </div>

          <div className="pt-3 border-t border-slate-100 space-y-2.5">
            <button
              onClick={() => {
                setMobileMenuOpen(false);
                onOpenContactModal();
              }}
              className="w-full py-3 px-4 rounded-full font-bold text-slate-700 bg-slate-100 hover:bg-slate-200 transition-colors flex items-center justify-center space-x-2"
            >
              <PhoneCall className="w-4 h-4 text-[#0A6EB4]" />
              <span>Contact & Operating Hours</span>
            </button>

            <button
              onClick={() => {
                setMobileMenuOpen(false);
                onOpenBooking();
              }}
              className="w-full py-3.5 px-4 rounded-full font-bold text-white bg-[#2FA8A1] hover:bg-[#28928b] shadow-md transition-all flex items-center justify-center space-x-2"
            >
              <Calendar className="w-5 h-5" />
              <span>Book Your Consultation</span>
            </button>
          </div>

          {/* Quick Phone call directly in mobile menu */}
          <div className="pt-2 text-center text-xs text-slate-500">
            Need urgent assistance? Call{' '}
            <a href={`tel:${PRACTICE_INFO.phone.replace(/\s+/g, '')}`} className="font-bold text-primary underline">
              {PRACTICE_INFO.phone}
            </a>
          </div>
        </div>
      )}
    </header>
  );
};
