import { db } from "@/lib/db";
import { clients, documents } from "@/lib/db/schema";
import { eq, desc, sql } from "drizzle-orm";
import { notFound } from "next/navigation";
import { ClientProfileClient } from "../../../_components/ClientProfileClient";

export default async function ClientProfilePage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;

  const [client] = await db.select().from(clients).where(eq(clients.id, id));
  if (!client) notFound();

  const docs = await db
    .select({
      id: documents.id,
      name: documents.name,
      status: documents.status,
      sentAt: documents.sentAt,
      clientId: documents.clientId,
      clientName: sql<string>`${clients.name}`,
    })
    .from(documents)
    .leftJoin(clients, eq(documents.clientId, clients.id))
    .where(eq(documents.clientId, id))
    .orderBy(desc(documents.createdAt));

  return <ClientProfileClient client={client} docs={docs} />;
}
