import { Users, AlertCircle } from 'lucide-react';
import { useGetXmemStatusQuery } from '../../store';
import { ActivityFeed } from './ActivityFeed';
import { BlockingMePanel } from './BlockingMePanel';
import { IAmBlockingPanel } from './IAmBlockingPanel';
import { SearchBox } from './SearchBox';

/*
 * Top-level Team Activity pane.
 * Renders nothing intrusive when memory-hub is disabled — just a one-line
 * hint with how to enable it. When enabled, shows three sub-panels + search.
 */
export function TeamActivityPane() {
  const { data: status, isLoading, isError } = useGetXmemStatusQuery(undefined, {
    pollingInterval: 30000,
  });

  if (isLoading) {
    return null;
  }

  if (isError || !status) {
    return (
      <section className="mt-8 glass-light rounded-xl p-4">
        <div className="flex items-center gap-2 text-sm text-red-400">
          <AlertCircle className="w-4 h-4" />
          <span>memory-hub status endpoint unreachable — is the backend running with /xmem mounted?</span>
        </div>
      </section>
    );
  }

  if (!status.memory.enabled || status.memory.backend === 'noop') {
    return (
      <section className="mt-8 glass-light rounded-xl p-4">
        <div className="flex items-center gap-3">
          <Users className="w-5 h-5 text-surface-500" />
          <div className="flex-1">
            <p className="text-sm text-surface-300">Cross-team sync is disabled</p>
            <p className="text-xs text-surface-500">
              Enable it via the <span className="font-mono">xmem_enable_sync</span> MCP tool, or
              edit <span className="font-mono">~/.priority-forge/xmem.json</span> and restart the
              backend. Default backend is <span className="font-mono">dryrun</span>; flip to
              <span className="font-mono"> xmem</span> once your hub credentials are valid.
            </p>
          </div>
          <span className="text-xs text-surface-500 font-mono">backend={status.memory.backend}</span>
        </div>
      </section>
    );
  }

  const backendBadge =
    status.memory.backend !== 'noop' && status.health.ok
      ? 'bg-green-500/20 text-green-400 border-green-500/40'
      : 'bg-yellow-500/20 text-yellow-400 border-yellow-500/40';

  return (
    <section className="mt-8 space-y-4">
      <div className="flex items-center gap-3">
        <Users className="w-5 h-5 text-blue-400" />
        <h2 className="text-sm font-medium text-surface-200 uppercase tracking-wider">
          Team Activity
        </h2>
        <span className="text-xs text-surface-500 font-mono">
          team={status.memory.teamId} · me={status.memory.agentId}
        </span>
        <span
          className={`ml-auto text-xs font-mono uppercase px-2 py-0.5 rounded border ${backendBadge}`}
        >
          backend={status.memory.backend}
          {!status.health.ok && ' · ⚠'}
        </span>
      </div>

      <div className="grid grid-cols-1 lg:grid-cols-3 gap-4">
        <ActivityFeed />
        <BlockingMePanel />
        <IAmBlockingPanel />
      </div>

      <SearchBox />

      {!status.health.ok && (
        <div className="text-xs text-yellow-500 font-mono">
          health: {status.health.detail ?? 'unhealthy'}
        </div>
      )}
    </section>
  );
}
