import { AppShell } from "@/components/layout/app-shell";
import { query } from "@/lib/db-postgres";

async function getProjectName(projectId: string): Promise<string> {
  try {
    const rows = await query<{ data: any }>(
      `SELECT data FROM fs_projects WHERE id = $1 LIMIT 1`,
      [projectId]
    );
    if (rows.length > 0) {
      const data = rows[0].data;
      return data?.productName || data?.name || "Project";
    }
  } catch (error) {
    console.error("Error fetching project name:", error);
  }
  return "Project";
}

export default async function ProjectLayout({
  children,
  params,
}: {
  children: React.ReactNode;
  params: Promise<{ workspace: string; projectId: string }>;
}) {
  const { workspace, projectId } = await params;
  const projectName = await getProjectName(projectId);

  return (
    <AppShell workspace={workspace} projectId={projectId} projectName={projectName}>
      {children}
    </AppShell>
  );
}
