import { notFound } from "next/navigation";
import { prisma } from "@/lib/db";
import { AGENT_LABEL, type AgentKind } from "@/lib/agents";
import AgentDetailClient from "./client";

export const dynamic = "force-dynamic";

export default async function AgentDetailPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const a = await prisma.agent.findUnique({
    where: { id },
    include: {
      host: true,
      events: { orderBy: { createdAt: "desc" }, take: 30 },
    },
  });
  if (!a) notFound();
  return (
    <div className="space-y-4">
      <div>
        <div className="text-xs text-muted">{AGENT_LABEL[a.kind as AgentKind]}</div>
        <h1 className="text-xl font-semibold">{a.displayName || a.name}</h1>
        {a.displayName && a.displayName !== a.name && (
          <div className="text-xs text-muted font-mono">tên hệ thống: {a.name}</div>
        )}
      </div>
      <AgentDetailClient
        initial={{
          id: a.id,
          name: a.name,
          displayName: a.displayName,
          kind: a.kind,
          hostId: a.hostId,
          hostName: a.host.name,
          hostAddr: a.host.host,
          installPath: a.installPath,
          status: a.status,
          version: a.version,
          pid: a.pid,
          autoStart: a.autoStart,
          notes: a.notes,
          configJson: a.configJson,
          declared: a.declared,
          deployMethod: a.deployMethod,
          deployRef: a.deployRef,
          uptimeSec: a.uptimeSec ?? null,
          restartCount: a.restartCount ?? null,
          updatedAt: a.updatedAt.toISOString(),
        }}
      />
    </div>
  );
}
