import { Shield } from 'lucide-react';
import { useGetXmemIAmBlockingQuery } from '../../store';

export function IAmBlockingPanel({ pollInterval = 15000 }: { pollInterval?: number }) {
  const { data, isLoading, isError } = useGetXmemIAmBlockingQuery(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">
        <Shield className="w-4 h-4 text-amber-400" />
        I'm Blocking
        {data && (
          <span className="ml-auto px-2 py-0.5 rounded bg-amber-500/20 text-amber-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">No one is waiting on 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-amber-500/5 border border-amber-500/20 animate-slide-up"
              style={{ animationDelay: `${i * 0.03}s`, animationFillMode: 'forwards' }}
            >
              <p className="text-sm text-surface-200">
                Your{' '}
                <span className="font-mono text-amber-400">{b.blockingTaskId}</span>{' '}
                is blocking
              </p>
              <p className="text-xs text-surface-500 mt-1">
                <span className="font-mono text-surface-300">{b.blockedTaskId}</span>{' '}
                ({b.blockedAgentId})
              </p>
            </div>
          ))}
        </div>
      )}
    </div>
  );
}
