'use client';

import Link from 'next/link';
import { useState, useRef, useEffect } from 'react';

interface AdminNotificationsBadgeProps {
  total: number;
  pendingOrders: number;
  designApprovals: number;
  photoRequests: number;
  unreadMessages: number;
}

export default function AdminNotificationsBadge({
  total,
  pendingOrders,
  designApprovals,
  photoRequests,
  unreadMessages,
}: AdminNotificationsBadgeProps) {
  const [showTooltip, setShowTooltip] = useState(false);
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const onClickOutside = (e: MouseEvent) => {
      if (ref.current && !ref.current.contains(e.target as Node)) {
        setShowTooltip(false);
      }
    };
    document.addEventListener('mousedown', onClickOutside);
    return () => document.removeEventListener('mousedown', onClickOutside);
  }, []);

  if (total === 0) return null;

  return (
    <div ref={ref} className="relative">
      <button
        onClick={() => setShowTooltip(!showTooltip)}
        className="relative inline-flex items-center justify-center w-8 h-8 rounded-full bg-splash-pink text-white text-xs font-bold hover:bg-splash-pink-dark transition-colors"
        title="Admin notifications"
      >
        {total}
      </button>

      {showTooltip && (
        <div className="absolute right-0 top-full mt-2 w-64 bg-paper border border-line rounded shadow-lg p-4 z-50 text-sm space-y-2">
          <p className="font-bold text-ink">Notifications</p>
          {pendingOrders > 0 && (
            <Link href="/admin/orders">
              <div className="flex items-center justify-between py-2 px-3 bg-surface rounded hover:bg-surface-dark cursor-pointer transition-colors">
                <span className="text-muted">Action needed</span>
                <span className="font-bold text-splash-orange">{pendingOrders}</span>
              </div>
            </Link>
          )}
          {designApprovals > 0 && (
            <Link href="/admin/designs">
              <div className="flex items-center justify-between py-2 px-3 bg-surface rounded hover:bg-surface-dark cursor-pointer transition-colors">
                <span className="text-muted">Design approvals</span>
                <span className="font-bold text-splash-blue">{designApprovals}</span>
              </div>
            </Link>
          )}
          {photoRequests > 0 && (
            <Link href="/admin/custom-requests">
              <div className="flex items-center justify-between py-2 px-3 bg-surface rounded hover:bg-surface-dark cursor-pointer transition-colors">
                <span className="text-muted">Photo requests</span>
                <span className="font-bold text-clay">{photoRequests}</span>
              </div>
            </Link>
          )}
          {unreadMessages > 0 && (
            <Link href="/admin/inbox">
              <div className="flex items-center justify-between py-2 px-3 bg-surface rounded hover:bg-surface-dark cursor-pointer transition-colors">
                <span className="text-muted">Unread messages</span>
                <span className="font-bold text-splash-pink">{unreadMessages}</span>
              </div>
            </Link>
          )}
        </div>
      )}
    </div>
  );
}
