import { ShieldAlert } from 'lucide-react';
import { useGetXmemBlockingMeQuery } from '../../store';

export function BlockingMePanel({ pollInterval = 15000 }: { pollInterval?: number }) {
  const { data, isLoading, isError } = useGetXmemBlockingMeQuery(undefined, {
    pollingInterval: pollInterval,
    refetchOnFocus: true,
  });

  return (
    <div className="glass-light rounded-xl p-4">
      <h3 className="text-xs font-medium text-surface-400 uppercase tracking-wider mb-3 flex items-center gap-2">
        <ShieldAlert className="w-4 h-4 text-red-400" />
        Blocking Me
        {data && (
          <span className="ml-auto px-2 py-0.5 rounded bg-red-500/20 text-red-400 text-xs">
            {data.count}
          </span>
        )}
      </h3>

      {isLoading && <p className="text-sm text-surface-500 text-center py-4">Loading…</p>}
      {isError && <p className="text-sm text-red-400 text-center py-4">Failed to load</p>}
      {data && data.count === 0 && (
        <p className="text-sm text-surface-500 text-center py-4">Nothing is blocking you 🎉</p>
      )}

      {data && data.count > 0 && (
        <div className="space-y-2 max-h-[300px] overflow-y-auto">
          {data.blockers.map((b, i) => (
            <div
              key={`${b.blockedTaskId}::${b.blockingTaskId}`}
              className="p-2 rounded-lg bg-red-500/5 border border-red-500/20 animate-slide-up"
              style={{ animationDelay: `${i * 0.03}s`, animationFillMode: 'forwards' }}
            >
              <p className="text-sm text-surface-200">
                <span className="font-mono text-red-400">{b.blockingTaskId}</span>{' '}
                <span className="text-surface-500">({b.blockingAgentId})</span>
              </p>
              <p className="text-xs text-surface-500 mt-1">
                blocking your task{' '}
                <span className="font-mono text-surface-300">{b.blockedTaskId}</span>
              </p>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}
