'use client';

import { useState, useRef, useEffect } from 'react';
import { useTranslations } from 'next-intl';
import { Link, usePathname } from '@/i18n/navigation';
import {
  Users,
  Building2,
  Package,
  FileText,
  BarChart3,
  Settings,
  ScrollText,
  Newspaper,
  type LucideIcon,
} from 'lucide-react';

interface AdminMenuItem {
  key: string;
  href: string;
  icon: LucideIcon;
}

const adminMenuItems: AdminMenuItem[] = [
  { key: 'users', href: '/dashboard/admin/users', icon: Users },
  { key: 'organizations', href: '/dashboard/admin/organizations', icon: Building2 },
  { key: 'bookings', href: '/dashboard/admin/bookings', icon: Package },
  { key: 'documents', href: '/dashboard/admin/documents', icon: FileText },
  { key: 'csvRates', href: '/dashboard/admin/csv-rates', icon: BarChart3 },
  { key: 'blog', href: '/dashboard/admin/blog', icon: Newspaper },
  { key: 'logs', href: '/dashboard/admin/logs', icon: ScrollText },
];

export default function AdminPanelDropdown() {
  const t = useTranslations('components.adminPanelDropdown');
  const [isOpen, setIsOpen] = useState(false);
  const dropdownRef = useRef<HTMLDivElement>(null);
  const pathname = usePathname();

  useEffect(() => {
    function handleClickOutside(event: MouseEvent) {
      if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
        setIsOpen(false);
      }
    }

    if (isOpen) {
      document.addEventListener('mousedown', handleClickOutside);
    }

    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [isOpen]);

  useEffect(() => {
    setIsOpen(false);
  }, [pathname]);

  const isActiveRoute = adminMenuItems.some(item => pathname.startsWith(item.href));

  return (
    <div className="relative" ref={dropdownRef}>
      {/* Trigger Button */}
      <button
        onClick={() => setIsOpen(!isOpen)}
        className={`flex items-center w-full px-4 py-3 text-sm font-medium rounded-lg transition-colors ${
          isActiveRoute ? 'bg-blue-50 text-blue-700' : 'text-gray-700 hover:bg-gray-100'
        }`}
      >
        <Settings className="mr-3 h-5 w-5" />
        <span className="flex-1 text-left">{t('trigger')}</span>
        <svg
          className={`w-4 h-4 transition-transform ${isOpen ? 'rotate-180' : ''}`}
          fill="none"
          stroke="currentColor"
          viewBox="0 0 24 24"
        >
          <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M19 9l-7 7-7-7" />
        </svg>
      </button>

      {/* Dropdown Menu */}
      {isOpen && (
        <div className="absolute left-0 right-0 mt-2 bg-white rounded-lg shadow-lg border border-gray-200 z-50 overflow-hidden">
          <div className="py-2">
            {adminMenuItems.map(item => {
              const isActive = pathname.startsWith(item.href);
              const IconComponent = item.icon;
              const name = t(`items.${item.key}` as any);
              const description = t(`items.${item.key}Desc` as any);
              return (
                <Link
                  key={item.key}
                  href={item.href}
                  className={`flex items-start px-4 py-3 hover:bg-gray-50 transition-colors ${
                    isActive ? 'bg-blue-50' : ''
                  }`}
                >
                  <IconComponent className="h-5 w-5 mr-3 mt-0.5 text-gray-500" />
                  <div className="flex-1">
                    <div
                      className={`text-sm font-medium ${isActive ? 'text-blue-700' : 'text-gray-900'}`}
                    >
                      {name}
                    </div>
                    <div className="text-xs text-gray-500 mt-0.5">{description}</div>
                  </div>
                </Link>
              );
            })}
          </div>
        </div>
      )}
    </div>
  );
}
