'use client';

import React, { useState } from 'react';
import {
  Calendar,
  Bell,
  Menu,
  ChevronDown,
  UserCheck,
  PhoneCall,
  MessageSquare,
  LogOut,
  Layers,
  ShieldCheck,
  Check
} from 'lucide-react';
import { AppUser, INITIAL_USERS } from '@/types/auth';

interface AAMovementTopHeaderProps {
  currentUser?: AppUser;
  onSwitchUser?: (user: AppUser) => void;
  onOpenCommunication?: () => void;
  onToggleMobileMenu?: () => void;
}

export const AAMovementTopHeader: React.FC<AAMovementTopHeaderProps> = ({
  currentUser,
  onSwitchUser,
  onOpenCommunication,
  onToggleMobileMenu
}) => {
  const [showRoleMenu, setShowRoleMenu] = useState(false);
  const [showNotifications, setShowNotifications] = useState(false);

  const activeUser = currentUser || {
    id: 'user-pu',
    pin: '21004',
    name: 'Fatima Yusuf',
    phone: '+2348021112222',
    role: 'PU_AGENT',
    assignedJurisdiction: 'PU-21-02-004 (Kanta Museum Square PU)',
    badgeId: 'SENTINEL-KB-004',
    puCode: 'PU-21-02-004',
    puName: 'Kanta Museum Square Polling Unit',
    ward: 'Argungu Central I',
    lga: 'Argungu',
    state: 'Kebbi'
  } as AppUser;

  const roleDisplayNames: Record<string, string> = {
    SITUATION_ROOM: 'National Coordinator (Situation Room)',
    STATE_SUPERVISOR: 'State Coordinator (Kebbi State)',
    LGA_COORDINATOR: 'LGA Coordinator (Argungu LGA)',
    WARD_SUPERVISOR: 'Ward Supervisor (Argungu Central I)',
    PU_AGENT: 'Polling Unit Agent (PU-21-02-004)',
    ICT_ADMIN: 'ICT Technical Administrator'
  };

  return (
    <header className="bg-white border-b border-slate-200/90 px-4 sm:px-6 py-3 flex items-center justify-between sticky top-0 z-30 shadow-xs">
      {/* Left: Mobile Toggle + Nigerian Flag Badge & Title */}
      <div className="flex items-center space-x-3.5">
        <button
          type="button"
          onClick={onToggleMobileMenu}
          className="lg:hidden p-2 rounded-xl text-slate-600 hover:bg-slate-100 transition-colors"
        >
          <Menu className="w-5 h-5" />
        </button>

        {/* Flag Badge & Title */}
        <div className="flex items-center space-x-3">
          {/* Nigeria Flag (Green-White-Green) */}
          <div className="w-8 h-6 rounded-md overflow-hidden flex shadow-xs border border-slate-200 flex-shrink-0">
            <div className="w-1/3 h-full bg-[#008751]" />
            <div className="w-1/3 h-full bg-white" />
            <div className="w-1/3 h-full bg-[#008751]" />
          </div>

          <div>
            <span className="text-[10px] font-bold text-slate-400 uppercase tracking-widest block leading-none">
              Nigeria
            </span>
            <h1 className="text-sm sm:text-base font-black text-slate-900 tracking-tight leading-tight">
              Presidential Election 2027
            </h1>
            <p className="text-[10px] text-slate-500 font-medium hidden sm:block">
              Election Monitoring System
            </p>
          </div>
        </div>
      </div>

      {/* Right: Date/Time Badge + Notifications + User Profile Pill + Quick Communication */}
      <div className="flex items-center space-x-2.5 sm:space-x-3.5">
        {/* Calendar / Clock Widget */}
        <div className="hidden md:flex items-center space-x-2 px-3 py-1.5 rounded-xl bg-slate-50 border border-slate-200/80 text-xs font-semibold text-slate-600">
          <Calendar className="w-3.5 h-3.5 text-slate-400" />
          <span>Sat, 16 Jan 2027</span>
          <span className="text-slate-300">•</span>
          <span className="font-mono text-slate-800 font-bold">4:32 PM (WAT)</span>
        </div>

        {/* Communication Quick Launcher */}
        <button
          type="button"
          onClick={onOpenCommunication}
          title="Open Multi-Tier Communication Hub"
          className="flex items-center space-x-1.5 px-3 py-1.5 rounded-xl bg-emerald-50 hover:bg-emerald-100 text-emerald-800 border border-emerald-300/80 text-xs font-bold transition-all shadow-xs"
        >
          <PhoneCall className="w-3.5 h-3.5 text-emerald-600" />
          <span className="hidden sm:inline">Comms Desk</span>
        </button>

        {/* Notification Bell */}
        <div className="relative">
          <button
            type="button"
            onClick={() => setShowNotifications(!showNotifications)}
            className="relative p-2 rounded-xl text-slate-600 hover:bg-slate-100 transition-colors"
          >
            <Bell className="w-4 h-4" />
            <span className="absolute top-1.5 right-1.5 w-2 h-2 rounded-full bg-rose-500 ring-2 ring-white" />
          </button>

          {/* Notifications Dropdown */}
          {showNotifications && (
            <div className="absolute right-0 mt-2 w-80 bg-white rounded-2xl shadow-2xl border border-slate-200 p-3 z-50 text-xs space-y-2">
              <div className="flex items-center justify-between border-b border-slate-100 pb-2">
                <span className="font-bold text-slate-800">Alerts & Notifications</span>
                <span className="px-1.5 py-0.5 rounded-full bg-rose-100 text-rose-700 font-bold text-[10px]">3 New</span>
              </div>
              <div className="space-y-1.5">
                <div className="p-2 rounded-xl bg-slate-50 border border-slate-100">
                  <span className="font-bold text-slate-800 block">Kano State e-EC8A</span>
                  <span className="text-slate-500 text-[11px]">8,140 PUs synchronized with 0 discrepancies.</span>
                </div>
                <div className="p-2 rounded-xl bg-rose-50 border border-rose-100">
                  <span className="font-bold text-rose-800 block">Over-voting Lock Prevented</span>
                  <span className="text-rose-600 text-[11px]">PU-04-12-008 flagged Section 51(2) violation.</span>
                </div>
                <div className="p-2 rounded-xl bg-emerald-50 border border-emerald-100">
                  <span className="font-bold text-emerald-800 block">Section 63 Recovery</span>
                  <span className="text-emerald-700 text-[11px]">18 ballots protected from ink smear discard.</span>
                </div>
              </div>
            </div>
          )}
        </div>

        {/* User Profile Pill with Role Teleporter */}
        <div className="relative">
          <button
            type="button"
            onClick={() => setShowRoleMenu(!showRoleMenu)}
            className="flex items-center space-x-2.5 p-1 sm:px-3 sm:py-1.5 rounded-xl bg-slate-50 hover:bg-slate-100 border border-slate-200 transition-all text-left"
          >
            <div className="w-7 h-7 rounded-full bg-emerald-600 text-white flex items-center justify-center font-bold text-xs shadow-xs">
              {activeUser.name.charAt(0)}
            </div>
            <div className="hidden md:block leading-tight">
              <span className="text-xs font-bold text-slate-900 block truncate max-w-[120px]">
                {activeUser.name}
              </span>
              <span className="text-[10px] font-semibold text-emerald-700 block truncate max-w-[140px]">
                {roleDisplayNames[activeUser.role] || activeUser.role.replace(/_/g, ' ')}
              </span>
            </div>
            <ChevronDown className="w-3.5 h-3.5 text-slate-400 hidden sm:block" />
          </button>

          {/* Role Switching Dropdown */}
          {showRoleMenu && (
            <div className="absolute right-0 mt-2 w-72 bg-white rounded-2xl shadow-2xl border border-slate-200 p-2 z-50 text-xs space-y-1">
              <div className="px-3 py-2 border-b border-slate-100">
                <span className="text-[10px] font-bold text-slate-400 uppercase tracking-wider block">
                  Switch Administrative Tier
                </span>
                <p className="text-slate-600 text-[11px] mt-0.5">
                  See how summaries reflect down from National to Ward level
                </p>
              </div>

              {INITIAL_USERS.map((user) => {
                const isSelected = activeUser.role === user.role;
                return (
                  <button
                    key={user.id}
                    type="button"
                    onClick={() => {
                      if (onSwitchUser) onSwitchUser(user);
                      setShowRoleMenu(false);
                    }}
                    className={`w-full flex items-center justify-between px-3 py-2 rounded-xl text-left transition-colors ${
                      isSelected
                        ? 'bg-emerald-50 text-emerald-900 font-bold border border-emerald-200'
                        : 'hover:bg-slate-50 text-slate-700'
                    }`}
                  >
                    <div>
                      <span className="block font-bold">{user.name}</span>
                      <span className="text-[10px] text-slate-400 font-medium block">
                        {roleDisplayNames[user.role] || user.role}
                      </span>
                    </div>
                    {isSelected && <Check className="w-4 h-4 text-emerald-600 flex-shrink-0" />}
                  </button>
                );
              })}
            </div>
          )}
        </div>
      </div>
    </header>
  );
};
