import { DashboardShell } from '../../../../components/dashboard-shell';
import { TemplateBuilderForm } from '../../../../components/template-builder-form';
import { requireAuthToken } from '../../../../lib/auth';
import { fetchTemplateById } from '../../../../lib/api';

type Props = {
  searchParams?: Promise<{
    id?: string;
  }>;
};

export default async function TemplateBuilderPage({ searchParams }: Props) {
  const token = await requireAuthToken();
  const resolvedSearchParams = (await searchParams) || {};
  const templateId = resolvedSearchParams.id?.trim();
  const template = templateId ? await fetchTemplateById(token, templateId) : null;

  return (
    <DashboardShell
      currentPath="/dashboard/templates"
      title="Admin Dashboard"
      searchPlaceholder="Search templates..."
    >
      <TemplateBuilderForm initialTemplate={template} />
    </DashboardShell>
  );
}
