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

export function DispatchHistory({
  events,
  failedTasks,
}: {
  events: TaskEvent[];
  failedTasks: TaskRecord[];
}) {
  return (
    <div className="space-y-6">
      <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>
                  <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 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>

      <Card>
        <CardHeader>
          <CardTitle>Failure Queue</CardTitle>
          <CardDescription>
            Tasks with a failed dispatch state that still require operator review or retry.
          </CardDescription>
        </CardHeader>
        <CardContent className="space-y-3">
          {failedTasks.length === 0 ? (
            <p className="text-sm text-slate-400">No failed dispatches recorded.</p>
          ) : (
            failedTasks.map((task) => (
              <div className="rounded-xl border border-amber-400/20 bg-amber-400/5 p-4" key={task.id}>
                <div className="flex flex-wrap items-center justify-between gap-3">
                  <div>
                    <p className="font-medium text-white">{task.title}</p>
                    <p className="text-sm text-slate-400">{task.assignee || "Unassigned"} • {task.target_host || "n/a"}</p>
                  </div>
                  <Badge variant="warning">{task.dispatch_state}</Badge>
                </div>
                <p className="mt-2 text-sm text-slate-300">{task.last_error || "No error text captured."}</p>
              </div>
            ))
          )}
        </CardContent>
      </Card>
    </div>
  );
}
