import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, CartesianGrid } from 'recharts';
import { Card, CardHeader, CardTitle, CardContent } from './ui/card';
import { Button } from './ui/button';
import { Input } from './ui/input';
import { Label } from './ui/label';
import { Alert, AlertDescription } from './ui/alert';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select';
import {
  ShieldAlert, Loader2, AlertCircle, CheckCircle, Ban, X, Trash2,
  BookOpen, Shield, Activity, Zap, RefreshCw, Globe, Terminal,
} from 'lucide-react';
import { Badge } from './ui/badge';
import { useCrowdSec } from '../hooks/useCrowdSec';
import type { AddDecisionRequest, CrowdSecAlert } from '../services/api/networking';

type Timescale = '1h' | '6h' | '24h' | '7d';

const TIMESCALES: { key: Timescale; label: string; ms: number; bucketMs: number }[] = [
  { key: '1h',  label: '1h',  ms: 60 * 60 * 1000,           bucketMs: 5 * 60 * 1000 },
  { key: '6h',  label: '6h',  ms: 6 * 60 * 60 * 1000,       bucketMs: 30 * 60 * 1000 },
  { key: '24h', label: '24h', ms: 24 * 60 * 60 * 1000,      bucketMs: 2 * 60 * 60 * 1000 },
  { key: '7d',  label: '7d',  ms: 7 * 24 * 60 * 60 * 1000,  bucketMs: 24 * 60 * 60 * 1000 },
];

const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;

function formatBucketLabel(ts: number, scale: Timescale): string {
  const d = new Date(ts);
  if (scale === '7d') return d.toLocaleDateString(undefined, { timeZone: tz, weekday: 'short', month: 'short', day: 'numeric' });
  return d.toLocaleTimeString(undefined, { timeZone: tz, hour: '2-digit', minute: '2-digit', hour12: false });
}

function binAlerts(alerts: CrowdSecAlert[], scale: Timescale) {
  const cfg = TIMESCALES.find(t => t.key === scale)!;
  const now = Date.now();
  // Align start to a clean bucket boundary so labels fall on round times
  const rawStart = now - cfg.ms;
  const alignedStart = Math.floor(rawStart / cfg.bucketMs) * cfg.bucketMs;
  const numBuckets = Math.ceil((now - alignedStart) / cfg.bucketMs);
  const buckets = Array.from({ length: numBuckets }, (_, i) => ({
    label: formatBucketLabel(alignedStart + i * cfg.bucketMs, scale),
    detections: 0,
  }));
  for (const a of alerts) {
    const t = new Date(a.createdAt).getTime();
    if (t < alignedStart) continue;
    const idx = Math.floor((t - alignedStart) / cfg.bucketMs);
    if (idx >= 0 && idx < numBuckets) buckets[idx].detections++;
  }
  return buckets;
}

function formatRelativeTime(isoString?: string): string {
  if (!isoString) return 'unknown';
  const diff = Date.now() - new Date(isoString).getTime();
  const minutes = Math.floor(diff / 60000);
  if (minutes < 1) return 'just now';
  if (minutes < 60) return `${minutes}m ago`;
  const hours = Math.floor(minutes / 60);
  if (hours < 24) return `${hours}h ago`;
  return `${Math.floor(hours / 24)}d ago`;
}

function scenarioHubUrl(scenario: string): string | null {
  if (!scenario) return null;
  const parts = scenario.split('/');
  // Only link hub scenarios in author/name format (e.g. crowdsecurity/http-probing-classb)
  // CAPI category labels (http:scan, ssh:bruteforce) have no individual hub pages
  if (parts.length === 2 && parts[0] !== 'custom') {
    return `https://app.crowdsec.net/hub/author/${parts[0]}/configurations/name/${parts[1]}`;
  }
  return null;
}

const KNOWN_ABBREV = new Set(['ssh', 'http', 'https', 'tcp', 'udp', 'ftp', 'ip', 'dns', 'tls', 'dos', 'ddos', 'bf']);

function titleCaseScenario(s: string): string {
  return s.replace(/[:\-_]/g, ' ').split(' ').filter(Boolean).map(w =>
    KNOWN_ABBREV.has(w.toLowerCase()) ? w.toUpperCase() : w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()
  ).join(' ');
}

function friendlyScenario(scenario: string, origin?: string): string {
  if (!scenario) {
    if (origin === 'capi') return 'Community blocklist';
    if (origin === 'manual' || origin === 'cscli') return 'Manual';
    return origin || 'Unknown';
  }
  const map: Record<string, string> = {
    'crowdsecurity/http-scan-classb': 'HTTP Scanner',
    'crowdsecurity/ssh-slow-bruteforce': 'SSH Brute Force (slow)',
    'crowdsecurity/ssh-bf': 'SSH Brute Force',
    'crowdsecurity/ssh-bruteforce': 'SSH Brute Force',
    'crowdsecurity/http-dos': 'HTTP DoS',
    'crowdsecurity/http-bruteforce': 'HTTP Brute Force',
    'crowdsecurity/http-exploit': 'HTTP Exploit',
    'crowdsecurity/http-crawl': 'HTTP Crawler',
    'crowdsecurity/http-bad-user-agent': 'Bad User Agent',
    'crowdsecurity/iptables-scan-multi_ports': 'Port Scanner',
  };
  if (map[scenario]) return map[scenario];
  const short = scenario.includes('/') ? scenario.split('/').slice(1).join(' ') : scenario;
  return titleCaseScenario(short);
}

function parseDuration(dur?: string): string {
  if (!dur) return '';
  // Go duration: "164h1m31s" → "6d 20h"
  const h = dur.match(/(\d+)h/);
  const m = dur.match(/(\d+)m/);
  const hours = h ? parseInt(h[1]) : 0;
  const mins = m ? parseInt(m[1]) : 0;
  if (hours >= 24) {
    const days = Math.floor(hours / 24);
    const rem = hours % 24;
    return rem > 0 ? `${days}d ${rem}h` : `${days}d`;
  }
  if (hours > 0) return mins > 0 ? `${hours}h ${mins}m` : `${hours}h`;
  return `${mins}m`;
}

interface BlockFormValues {
  ip: string;
  type: 'ban' | 'allow';
  duration: string;
  reason: string;
}

export function CrowdSecComponent() {
  const {
    status,
    isLoadingStatus,
    summary,
    isLoadingSummary,
    alerts,
    isLoadingAlerts,
    refetchAlerts,
    graphAlerts,
    isLoadingGraphAlerts,
    decisions,
    addDecision,
    isAddingDecision,
    addDecisionError,
    deleteDecision,
    deleteBouncer,
    deleteMachine,
  } = useCrowdSec();

  const [blockSuccess, setBlockSuccess] = useState<string | null>(null);
  const [timescale, setTimescale] = useState<Timescale>('24h');

  const { register, handleSubmit, reset, setValue, watch, formState: { errors } } = useForm<BlockFormValues>({
    defaultValues: { ip: '', type: 'ban', duration: '24h', reason: 'manual' },
  });
  const selectedType = watch('type');

  function onBlockSubmit(values: BlockFormValues) {
    setBlockSuccess(null);
    const req: AddDecisionRequest = { ip: values.ip, type: values.type, reason: values.reason, duration: values.duration };
    addDecision(req, {
      onSuccess: (data) => {
        setBlockSuccess(data.message);
        reset();
      },
    });
  }

  const sortedReasons = summary
    ? Object.entries(summary.byReason).sort(([, a], [, b]) => b - a)
    : [];

  return (
    <div className="space-y-6">
      <div className="flex items-center gap-4 mb-6">
        <div className="p-2 bg-primary/10 rounded-lg">
          <ShieldAlert className="h-6 w-6 text-primary" />
        </div>
        <div>
          <h2 className="text-2xl font-semibold">CrowdSec</h2>
          <p className="text-muted-foreground">Collaborative intrusion detection — protecting your infrastructure with community threat intelligence</p>
        </div>
      </div>

      {/* Educational card */}
      <Card className="bg-gradient-to-br from-cyan-50 to-blue-50 dark:from-cyan-900/20 dark:to-blue-900/20 border-cyan-200 dark:border-cyan-800">
        <CardContent className="pt-4">
          <div className="flex gap-3">
            <BookOpen className="h-5 w-5 text-cyan-600 dark:text-cyan-400 shrink-0 mt-0.5" />
            <div className="space-y-1 text-sm text-cyan-900 dark:text-cyan-100">
              <p>
                <strong>CrowdSec</strong> analyzes logs from your k8s clusters, detects attacks, and shares threat
                intelligence with the community. Wild Central runs the <strong>LAPI</strong> — the hub that agents
                report to and that bouncers query for the live blocklist. Your cluster contributes to and benefits
                from a shared blocklist of millions of known malicious IPs.
              </p>
            </div>
          </div>
        </CardContent>
      </Card>

      {/* LAPI Status */}
      <Card>
        <CardHeader>
          <div className="flex items-center justify-between">
            <div className="flex items-center gap-2">
              <Shield className="h-4 w-4 text-muted-foreground" />
              <CardTitle>LAPI Status</CardTitle>
            </div>
            {isLoadingStatus ? (
              <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
            ) : (
              <Badge variant={status?.active ? 'success' : 'warning'} className="gap-1">
                {status?.active && <CheckCircle className="h-3 w-3" />}
                {status?.active ? 'Running' : 'Stopped'}
              </Badge>
            )}
          </div>
        </CardHeader>
        <CardContent className="space-y-3">
          {!status?.active && !isLoadingStatus && (
            <p className="text-sm text-muted-foreground">
              CrowdSec is not running on Wild Central. Install it with{' '}
              <code className="font-mono text-xs bg-muted px-1 rounded">apt install crowdsec crowdsec-firewall-bouncer</code>
              {' '}or reinstall wild-cloud-central.
            </p>
          )}
          {status?.active && (
            <>
              <div className="flex items-center justify-between py-2 px-3 rounded bg-muted">
                <div className="flex items-center gap-2 text-sm">
                  <Shield className="h-4 w-4 text-muted-foreground" />
                  <span>Perimeter firewall enforcement</span>
                  <span className="text-xs text-muted-foreground">(nftables on Wild Central)</span>
                </div>
                <Badge variant={status.firewallBouncer ? 'success' : 'warning'} className="gap-1">
                  {status.firewallBouncer && <CheckCircle className="h-3 w-3" />}
                  {status.firewallBouncer ? 'Active' : 'Stopped'}
                </Badge>
              </div>
              {!status.firewallBouncer && (
                <p className="text-xs text-muted-foreground">
                  Install the firewall bouncer to block threats at the perimeter:{' '}
                  <code className="font-mono bg-muted px-1 rounded">apt install crowdsec-firewall-bouncer</code>
                </p>
              )}
              <p className="text-xs text-muted-foreground">
                {status.machines.length > 0 && `${status.machines.length} agent${status.machines.length !== 1 ? 's' : ''} reporting. `}
                {(() => {
                  const count = status.bouncers.filter(b => !b.name.includes('@')).length;
                  return count > 0 && `${count} bouncer${count !== 1 ? 's' : ''} active.`;
                })()}
              </p>
            </>
          )}
        </CardContent>
      </Card>

      {status?.active && (
        <>
          {/* Active Protection Summary */}
          <Card>
            <CardHeader>
              <div className="flex items-center gap-2">
                <Activity className="h-4 w-4 text-muted-foreground" />
                <CardTitle>Active Protection</CardTitle>
              </div>
            </CardHeader>
            <CardContent className="space-y-3">
              <p className="text-sm text-muted-foreground">
                IPs currently blocked by the community blocklist (CAPI) and local decisions, enforced by your bouncers.
              </p>
              {isLoadingSummary ? (
                <div className="flex items-center gap-2 text-sm text-muted-foreground">
                  <Loader2 className="h-4 w-4 animate-spin" />Loading ban statistics…
                </div>
              ) : summary ? (
                <div className="space-y-3">
                  <div className="flex items-center gap-3">
                    <div className="flex items-center gap-2">
                      <Ban className="h-5 w-5 text-red-500" />
                      <span className="text-2xl font-bold">{summary.total.toLocaleString()}</span>
                    </div>
                    <span className="text-sm text-muted-foreground">IPs blocked right now</span>
                  </div>
                  {sortedReasons.length > 0 && (
                    <div className="space-y-1.5">
                      <p className="text-xs font-medium text-muted-foreground uppercase tracking-wide">By threat type</p>
                      {sortedReasons.slice(0, 8).map(([reason, count]) => {
                        const pct = Math.round((count / summary.total) * 100);
                        return (
                          <div key={reason} className="flex items-center gap-2 text-sm">
                            <div className="flex-1 flex items-center gap-2">
                              <div
                                className="h-1.5 bg-red-400 rounded"
                                style={{ width: `${Math.max(pct, 2)}%`, maxWidth: '60%' }}
                              />
                              {scenarioHubUrl(reason) ? (
                                <a href={scenarioHubUrl(reason)!} target="_blank" rel="noopener noreferrer" className="text-muted-foreground hover:text-foreground hover:underline">
                                  {friendlyScenario(reason)}
                                </a>
                              ) : (
                                <span className="text-muted-foreground">{friendlyScenario(reason)}</span>
                              )}
                            </div>
                            <span className="font-mono text-xs tabular-nums">{count.toLocaleString()}</span>
                            <span className="text-xs text-muted-foreground w-8 text-right">{pct}%</span>
                          </div>
                        );
                      })}
                      {sortedReasons.length > 8 && (
                        <p className="text-xs text-muted-foreground">+{sortedReasons.length - 8} more categories</p>
                      )}
                    </div>
                  )}
                </div>
              ) : null}
            </CardContent>
          </Card>

          {/* Detections Over Time */}
          <Card>
            <CardHeader>
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <Activity className="h-4 w-4 text-muted-foreground" />
                  <CardTitle>Detections Over Time</CardTitle>
                </div>
                <div className="flex gap-1">
                  {TIMESCALES.map(t => (
                    <Button
                      key={t.key}
                      variant={timescale === t.key ? 'default' : 'ghost'}
                      size="sm"
                      className="h-7 px-2 text-xs"
                      onClick={() => setTimescale(t.key)}
                    >
                      {t.label}
                    </Button>
                  ))}
                </div>
              </div>
            </CardHeader>
            <CardContent>
              {isLoadingGraphAlerts ? (
                <div className="flex items-center justify-center h-40 gap-2 text-sm text-muted-foreground">
                  <Loader2 className="h-4 w-4 animate-spin" />Loading…
                </div>
              ) : (
                <ResponsiveContainer width="100%" height={160}>
                  <BarChart data={binAlerts(graphAlerts, timescale)} margin={{ top: 4, right: 8, left: -20, bottom: 0 }}>
                    <CartesianGrid strokeDasharray="3 3" className="stroke-muted" />
                    <XAxis dataKey="label" tick={{ fontSize: 10 }} interval="preserveStartEnd" className="fill-muted-foreground" />
                    <YAxis allowDecimals={false} tick={{ fontSize: 10 }} className="fill-muted-foreground" />
                    <Tooltip
                      contentStyle={{ fontSize: 12 }}
                      formatter={(v) => [v, 'detections']}
                    />
                    <Bar dataKey="detections" fill="#ef4444" radius={[2, 2, 0, 0]} />
                  </BarChart>
                </ResponsiveContainer>
              )}
            </CardContent>
          </Card>

          {/* Recent Alert Feed */}
          <Card>
            <CardHeader>
              <div className="flex items-center justify-between">
                <div className="flex items-center gap-2">
                  <Activity className="h-4 w-4 text-muted-foreground" />
                  <CardTitle>Recent Detections</CardTitle>
                </div>
                <Button variant="ghost" size="sm" onClick={() => refetchAlerts()} className="h-7 gap-1.5 text-xs">
                  <RefreshCw className="h-3 w-3" />Refresh
                </Button>
              </div>
            </CardHeader>
            <CardContent>
              <p className="text-sm text-muted-foreground mb-3">
                Attack events detected by your agents and reported to this LAPI. Each event results in a ban
                that is shared with the CrowdSec community.
              </p>
              {isLoadingAlerts ? (
                <div className="flex items-center gap-2 text-sm text-muted-foreground">
                  <Loader2 className="h-4 w-4 animate-spin" />Loading…
                </div>
              ) : alerts.length === 0 ? (
                <div className="text-center py-6">
                  <Shield className="h-10 w-10 text-muted-foreground mx-auto mb-2" />
                  <p className="text-sm text-muted-foreground">No detections yet.</p>
                  <p className="text-xs text-muted-foreground mt-1">
                    Events appear here when agents detect and report attacks. The community blocklist
                    (CAPI) is already protecting you even before local detections.
                  </p>
                </div>
              ) : (
                <div className="space-y-1 max-h-72 overflow-y-auto">
                  <div className="grid grid-cols-[1fr_auto_auto_auto] gap-x-3 text-xs text-muted-foreground font-medium px-2 pb-1">
                    <span>Source IP</span>
                    <span>Threat</span>
                    <span>Agent</span>
                    <span>When</span>
                  </div>
                  {alerts.map((alert) => (
                    <div key={alert.id} className="grid grid-cols-[1fr_auto_auto_auto] gap-x-3 items-center py-1.5 px-2 bg-muted rounded text-sm">
                      <div className="flex items-center gap-1.5 min-w-0">
                        <span className="font-mono text-xs truncate">{alert.sourceIP || '—'}</span>
                        {alert.country && (
                          <span className="text-xs text-muted-foreground flex items-center gap-0.5 shrink-0">
                            <Globe className="h-3 w-3" />{alert.country}
                          </span>
                        )}
                      </div>
                      {scenarioHubUrl(alert.scenario) ? (
                        <a href={scenarioHubUrl(alert.scenario)!} target="_blank" rel="noopener noreferrer" className="text-xs text-muted-foreground whitespace-nowrap hover:text-foreground hover:underline">
                          {friendlyScenario(alert.scenario)}
                        </a>
                      ) : (
                        <span className="text-xs text-muted-foreground whitespace-nowrap">{friendlyScenario(alert.scenario)}</span>
                      )}
                      <span className="text-xs font-mono text-muted-foreground whitespace-nowrap">{alert.machineId?.replace(/^wc-/, '') ?? '—'}</span>
                      <span className="text-xs text-muted-foreground whitespace-nowrap">{formatRelativeTime(alert.createdAt)}</span>
                    </div>
                  ))}
                </div>
              )}
            </CardContent>
          </Card>

          {/* Manual Decisions: Block / Allow */}
          <Card>
            <CardHeader>
              <div className="flex items-center gap-2">
                <Ban className="h-4 w-4 text-muted-foreground" />
                <CardTitle>Block or Allow an IP</CardTitle>
              </div>
            </CardHeader>
            <CardContent className="space-y-4">
              <p className="text-sm text-muted-foreground">
                Manually ban an IP (e.g. an attacker you've identified) or add a local allowlist entry
                to prevent a legitimate IP from being blocked by the community list.
              </p>

              <form onSubmit={handleSubmit(onBlockSubmit)} className="space-y-3">
                <div className="grid grid-cols-2 gap-3">
                  <div>
                    <Label htmlFor="ip">IP Address</Label>
                    <Input
                      id="ip"
                      placeholder="1.2.3.4"
                      className="mt-1 font-mono"
                      {...register('ip', {
                        required: 'Required',
                        pattern: { value: /^[\d.:/a-fA-F]+$/, message: 'Invalid IP' },
                      })}
                    />
                    {errors.ip && <p className="text-xs text-red-600 mt-1">{errors.ip.message}</p>}
                  </div>
                  <div>
                    <Label htmlFor="reason">Reason</Label>
                    <Input
                      id="reason"
                      placeholder="manual"
                      className="mt-1"
                      {...register('reason')}
                    />
                  </div>
                </div>

                <div className="grid grid-cols-2 gap-3">
                  <div>
                    <Label>Action</Label>
                    <Select value={selectedType} onValueChange={(v) => setValue('type', v as 'ban' | 'allow')}>
                      <SelectTrigger className="mt-1">
                        <SelectValue />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="ban">Ban (block)</SelectItem>
                        <SelectItem value="allow">Allow (whitelist)</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>
                  <div>
                    <Label>Duration</Label>
                    <Select defaultValue="24h" onValueChange={(v) => setValue('duration', v)}>
                      <SelectTrigger className="mt-1">
                        <SelectValue />
                      </SelectTrigger>
                      <SelectContent>
                        <SelectItem value="1h">1 hour</SelectItem>
                        <SelectItem value="24h">24 hours</SelectItem>
                        <SelectItem value="168h">7 days</SelectItem>
                        <SelectItem value="720h">30 days</SelectItem>
                        <SelectItem value="8760h">1 year</SelectItem>
                      </SelectContent>
                    </Select>
                  </div>
                </div>

                {blockSuccess && (
                  <Alert>
                    <CheckCircle className="h-4 w-4" />
                    <AlertDescription className="text-xs">{blockSuccess}</AlertDescription>
                  </Alert>
                )}
                {addDecisionError && (
                  <Alert variant="error">
                    <AlertCircle className="h-4 w-4" />
                    <AlertDescription className="text-xs">{(addDecisionError as Error).message}</AlertDescription>
                  </Alert>
                )}

                <Button type="submit" size="sm" disabled={isAddingDecision} className="gap-2">
                  {isAddingDecision ? <Loader2 className="h-4 w-4 animate-spin" /> : <Ban className="h-4 w-4" />}
                  {selectedType === 'allow' ? 'Add to Allowlist' : 'Block IP'}
                </Button>
              </form>

              {/* Local decisions list */}
              {decisions.length > 0 && (
                <div className="border-t pt-3 space-y-2">
                  <p className="text-xs font-medium text-muted-foreground uppercase tracking-wide">Active local decisions</p>
                  <div className="space-y-1 max-h-48 overflow-y-auto">
                    {decisions.map((d) => (
                      <div key={d.id} className="flex items-center justify-between py-1.5 px-2 bg-muted rounded text-xs">
                        <div className="flex items-center gap-2 min-w-0">
                          {d.type === 'allow'
                            ? <CheckCircle className="h-3 w-3 text-green-500 shrink-0" />
                            : <Ban className="h-3 w-3 text-red-500 shrink-0" />
                          }
                          <span className="font-mono truncate">{d.value}</span>
                          <Badge variant="outline" className="text-xs h-4 shrink-0">{d.type}</Badge>
                          <span className="text-muted-foreground truncate">{friendlyScenario(d.scenario, d.origin)}</span>
                        </div>
                        <div className="flex items-center gap-2 shrink-0">
                          {d.duration && (
                            <span className="text-muted-foreground">{parseDuration(d.duration)}</span>
                          )}
                          <Button
                            variant="ghost"
                            size="sm"
                            className="h-5 w-5 p-0 text-muted-foreground hover:text-red-500"
                            onClick={() => deleteDecision(d.id)}
                          >
                            <X className="h-3 w-3" />
                          </Button>
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              )}
            </CardContent>
          </Card>

          {/* Registered Agents */}
          <Card>
            <CardHeader>
              <div className="flex items-center gap-2">
                <Zap className="h-4 w-4 text-muted-foreground" />
                <CardTitle>Registered Agents</CardTitle>
              </div>
            </CardHeader>
            <CardContent className="space-y-3">
              <p className="text-sm text-muted-foreground">
                <strong>Agents</strong> are CrowdSec engines that analyze logs, detect threats, and report
                to this LAPI. Each machine or cluster running CrowdSec registers an agent here.
              </p>
              {status.machines.length === 0 ? (
                <p className="text-sm text-muted-foreground text-center py-3">No agents registered. Provision an instance below.</p>
              ) : (
                <div className="space-y-2">
                  {status.machines.map((m) => (
                    <div key={m.machineId} className="flex items-center justify-between py-2 px-3 bg-muted rounded">
                      <div className="space-y-0.5">
                        <div className="flex items-center gap-2">
                          <span className="font-mono text-sm">{m.machineId}</span>
                          <Badge variant={m.isValidated ? 'default' : 'secondary'} className="text-xs h-4">
                            {m.isValidated ? 'validated' : 'pending'}
                          </Badge>
                        </div>
                        <div className="flex items-center gap-3 text-xs text-muted-foreground">
                          {m.last_heartbeat && <span>heartbeat {formatRelativeTime(m.last_heartbeat)}</span>}
                          {m.version && <span>{m.version}</span>}
                          {m.ipAddress && <span className="font-mono">{m.ipAddress}</span>}
                        </div>
                      </div>
                      <Button
                        variant="ghost"
                        size="sm"
                        className="h-7 w-7 p-0 text-muted-foreground hover:text-red-500"
                        onClick={() => deleteMachine(m.machineId)}
                      >
                        <Trash2 className="h-3 w-3" />
                      </Button>
                    </div>
                  ))}
                </div>
              )}
            </CardContent>
          </Card>

          {/* Registered Bouncers */}
          <Card>
            <CardHeader>
              <div className="flex items-center gap-2">
                <Shield className="h-4 w-4 text-muted-foreground" />
                <CardTitle>Registered Bouncers</CardTitle>
              </div>
            </CardHeader>
            <CardContent className="space-y-3">
              <p className="text-sm text-muted-foreground">
                <strong>Bouncers</strong> enforce the blocklist. Wild Central's nftables bouncer blocks at
                the network perimeter. Each k8s cluster also runs a Traefik bouncer as a second layer.
              </p>
              {status.bouncers.length === 0 ? (
                <p className="text-sm text-muted-foreground text-center py-3">No bouncers registered. Provision an instance to add them.</p>
              ) : (
                <div className="space-y-2">
                  {(() => {
                    // Group auto-created child bouncers (name contains @) under their parent
                    const parents = status.bouncers.filter(b => !b.name.includes('@'));
                    const children = status.bouncers.filter(b => b.name.includes('@'));

                    return parents.map((b) => {
                      const isFirewallBouncer = b.type === 'crowdsec-firewall-bouncer';
                      const childBouncers = children.filter(c => c.name.startsWith(b.name + '@'));
                      return (
                        <div key={b.name} className="py-2 px-3 bg-muted rounded">
                          <div className="flex items-center justify-between">
                            <div className="space-y-0.5">
                              <div className="flex items-center gap-2">
                                <span className="font-mono text-sm truncate max-w-64">{b.name}</span>
                                <Badge variant={b.revoked ? 'secondary' : 'default'} className="text-xs h-4">
                                  {b.revoked ? 'revoked' : 'active'}
                                </Badge>
                                {isFirewallBouncer && (
                                  <Badge variant="outline" className="text-xs h-4 border-orange-400 text-orange-600 dark:text-orange-400">
                                    perimeter
                                  </Badge>
                                )}
                              </div>
                              <p className="text-xs text-muted-foreground">
                                {isFirewallBouncer ? 'Crowdsec Firewall Bouncer' : b.type?.replace(/-/g, ' ') || ''}
                              </p>
                            </div>
                            {!isFirewallBouncer && (
                              <Button
                                variant="ghost"
                                size="sm"
                                className="h-7 w-7 p-0 text-muted-foreground hover:text-red-500"
                                onClick={() => deleteBouncer(b.name)}
                              >
                                <Trash2 className="h-3 w-3" />
                              </Button>
                            )}
                          </div>
                          {childBouncers.length > 0 && (
                            <div className="mt-1.5 ml-4 space-y-1 border-l-2 border-muted-foreground/20 pl-3">
                              {childBouncers.map(c => {
                                const ip = c.name.split('@')[1];
                                return (
                                  <div key={c.name} className="flex items-center gap-2 text-xs text-muted-foreground">
                                    <span className="font-mono">{ip}</span>
                                    <Badge variant={c.revoked ? 'secondary' : 'default'} className="text-[10px] h-3.5">
                                      {c.revoked ? 'revoked' : 'active'}
                                    </Badge>
                                  </div>
                                );
                              })}
                            </div>
                          )}
                        </div>
                      );
                    });
                  })()}
                </div>
              )}
            </CardContent>
          </Card>

          {/* Registering Agents */}
          <Card className="border-dashed">
            <CardHeader>
              <div className="flex items-center gap-2">
                <Terminal className="h-4 w-4 text-muted-foreground" />
                <CardTitle>Registering New Agents</CardTitle>
              </div>
            </CardHeader>
            <CardContent className="space-y-3 text-sm text-muted-foreground">
              <p>
                To connect a new CrowdSec agent to this LAPI, register a machine and bouncer via the API,
                then configure the remote agent to use these credentials.
              </p>
              <div className="space-y-2">
                <p className="font-medium text-foreground">1. Register a machine (agent)</p>
                <pre className="bg-muted rounded-md p-3 font-mono text-xs overflow-x-auto">
{`curl -X POST /api/v1/crowdsec/machines \\
  -H "Content-Type: application/json" \\
  -d '{"name": "my-agent", "password": "a-strong-password"}'`}
                </pre>
              </div>
              <div className="space-y-2">
                <p className="font-medium text-foreground">2. Register a bouncer</p>
                <pre className="bg-muted rounded-md p-3 font-mono text-xs overflow-x-auto">
{`curl -X POST /api/v1/crowdsec/bouncers \\
  -H "Content-Type: application/json" \\
  -d '{"name": "my-bouncer", "apiKey": "a-bouncer-api-key"}'`}
                </pre>
              </div>
              <p>
                Then configure the remote CrowdSec agent with the LAPI URL (<code className="font-mono bg-muted px-1 rounded">http://{'<central-ip>'}:8080</code>)
                and the credentials above. The agent will appear in the Registered Agents list once it connects.
              </p>
            </CardContent>
          </Card>

        </>
      )}
    </div>
  );
}
