import type { Metadata } from "next";
import { createPostAction } from "@/actions/posts";
import { PostForm } from "@/components/admin/PostForm";
import { requireUser } from "@/lib/auth/dal";
import { listAllTags } from "@/lib/services/tags";
import { getUserWithTags } from "@/lib/services/users";

export const metadata: Metadata = { title: "New post" };

export default async function NewPostPage() {
  const user = await requireUser();
  const isAdmin = user.role === "admin";
  // Authors only ever see (and can only use) their granted tags.
  const allTags = isAdmin
    ? await listAllTags()
    : ((await getUserWithTags(user.id))?.tags ?? []);

  return (
    <div>
      <h1 className="mb-6 text-2xl font-bold tracking-tight text-ink-bright">New post</h1>
      <PostForm
        allTags={allTags}
        defaultAuthor={user.username}
        canCreateTags={user.permissions.createTags}
        statusOptions={user.permissions.publishPosts ? ["draft", "published"] : ["draft"]}
        action={createPostAction}
      />
    </div>
  );
}
