import React from 'react';
import { ScanLine } from 'lucide-react';
import { agentRegistryEntry } from '@/lib/agentTypes';

const label = (value) => String(value || 'unknown').replaceAll('_', ' ');

export default function AgentCard({ status, onOpen }) {
  const data = status || {};
  const ready = data.kinectState === 'ready' && data.bridgeState === 'connected';
  const state = data.operation && data.operation !== 'idle'
    ? label(data.operation)
    : ready ? 'Ready' : label(data.agentState || 'Unavailable');

  return (
    <article className="rounded-2xl border border-white/10 bg-[#111114] p-5">
      <div className="flex items-start gap-4">
        <div className="flex h-12 w-12 items-center justify-center rounded-xl border border-purple-400/30 bg-black shadow-[0_0_18px_rgba(168,85,247,.18)]"><ScanLine className="text-[#7dff72]"/></div>
        <div className="min-w-0 flex-1">
          <h3 className="font-semibold text-white">{agentRegistryEntry.name}</h3>
          <p className="mt-1 text-sm text-zinc-400">{agentRegistryEntry.description}</p>
          <p className="mt-1 text-[10px] uppercase tracking-wider text-zinc-600">{agentRegistryEntry.category} · {agentRegistryEntry.provider}</p>
          <p className="mt-2 inline-flex items-center gap-1.5 rounded-full border border-purple-400/30 bg-purple-400/10 px-2 py-0.5 text-[10px] font-mono text-purple-200">{agentRegistryEntry.requiredCapability}</p>
        </div>
      </div>
      <dl className="mt-5 grid grid-cols-2 gap-2 text-xs">
        <div className="rounded-lg bg-black/30 p-2"><dt className="text-zinc-600">Agent status</dt><dd className="mt-1 capitalize text-zinc-200">{state}</dd></div>
        <div className="rounded-lg bg-black/30 p-2"><dt className="text-zinc-600">Bridge</dt><dd className="mt-1 capitalize text-zinc-200">{label(data.bridgeState || 'offline')}</dd></div>
        <div className="rounded-lg bg-black/30 p-2"><dt className="text-zinc-600">Kinect</dt><dd className="mt-1 capitalize text-zinc-200">{label(data.kinectState || 'not_detected')}</dd></div>
        <div className="rounded-lg bg-black/30 p-2"><dt className="text-zinc-600">Current project</dt><dd className="mt-1 truncate text-zinc-200">{data.activeProjectName || 'None'}</dd></div>
        <div className="col-span-2 rounded-lg bg-black/30 p-2"><dt className="text-zinc-600">Last successful scan</dt><dd className="mt-1 text-zinc-200">{data.lastSuccessfulScan ? new Date(data.lastSuccessfulScan).toLocaleString() : 'No completed scans'}</dd></div>
      </dl>
      <p className="mt-4 text-[11px] leading-relaxed text-zinc-600">Ready is shown only after the ScanForge Agent confirms both the bridge and Kinect depth stream.</p>
      <button onClick={onOpen} className="mt-3 w-full rounded-xl bg-[#7dff72] py-3 text-sm font-bold text-black">Open Scanner</button>
    </article>
  );
}
