import { submitUserVideoAction } from "@/lib/actions";
import { requireApprovedUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { ShortUploadForm } from "@/components/user/ShortUploadForm";
import { UserShell } from "@/components/user/UserShell";

type NewUserShortPageProps = {
  searchParams: Promise<{ error?: string }>;
};

const errorMessages: Record<string, string> = {
  required: "Please enter a title, valid Shorts URL, and description.",
  youtube: "Please paste a valid YouTube Shorts link."
};

export default async function NewUserShortPage({ searchParams }: NewUserShortPageProps) {
  const session = await requireApprovedUser();
  const [params, categories] = await Promise.all([
    searchParams,
    prisma.category.findMany({ orderBy: { name: "asc" } })
  ]);

  const errorText = params.error ? errorMessages[params.error] ?? errorMessages.required : null;

  return (
    <UserShell session={session}>
      <ShortUploadForm
        action={submitUserVideoAction}
        backHref="/dashboard"
        categories={categories}
        error={errorText}
      />
    </UserShell>
  );
}
