import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import type { TaskEvent } from "@/lib/types";

export function DispatchHistory({
  events,
}: {
  events: TaskEvent[];
}) {
  return (
    <Card>
      <CardHeader>
        <CardTitle>Dispatch History</CardTitle>
        <CardDescription>
          Every dispatch request, success, failure, and acknowledgement recorded by the control plane.
        </CardDescription>
      </CardHeader>
      <CardContent className="space-y-3">
        {events.map((event) => (
          <div className="rounded-xl border border-white/10 bg-slate-950/40 p-4" key={event.id}>
            <div className="flex flex-wrap items-center justify-between gap-3">
              <div className="min-w-0">
                <p className="font-medium text-white">{event.summary}</p>
                <p className="text-sm text-slate-400">
                  Task #{event.task_id} • {event.assignee || "unassigned"} • {event.host || "n/a"}
                </p>
              </div>
              <div className="flex gap-2">
                <Badge variant={event.family === "zeroclaw" ? "success" : event.family === "direct" ? "warning" : "default"}>
                  {event.family || "manual"}
                </Badge>
                <Badge variant={event.event_type === "dispatch_failed" ? "warning" : "secondary"}>
                  {event.event_type}
                </Badge>
              </div>
            </div>
            {event.detail ? <p className="mt-2 break-words text-sm text-slate-300">{event.detail}</p> : null}
            <p className="mt-2 text-xs uppercase tracking-[0.2em] text-slate-500">{event.created_at}</p>
          </div>
        ))}
      </CardContent>
    </Card>
  );
}
