import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { PostForm } from "@/components/admin/PostForm";
import { getPost, getAllPosts } from "@/lib/cms";

export const metadata: Metadata = {
  title: "Edit journal post",
  robots: { index: false, follow: false },
};

export default async function EditPostPage({
  params,
}: {
  params: Promise<{ slug: string }>;
}) {
  const { slug } = await params;
  const [post, posts] = await Promise.all([getPost(slug), getAllPosts()]);
  if (!post) notFound();

  const categories = [...new Set(posts.map((p) => p.category))];

  const initial = {
    slug: post.slug,
    title: post.title,
    excerpt: post.excerpt,
    date: post.date,
    readTime: post.readTime,
    category: post.category,
    coverImage: post.coverImage,
    content: post.content,
    published: post.published,
    metaTitle: post.seo.metaTitle,
    metaDescription: post.seo.metaDescription,
    keywords: post.seo.keywords,
    ogImage: post.seo.ogImage,
    noindex: post.seo.noindex,
  };

  return (
    <div className="space-y-8">
      <header>
        <p className="text-sm font-semibold uppercase tracking-[0.14em] text-coral">
          Content
        </p>
        <h1 className="display-2 mt-2">Edit journal post</h1>
        <p className="mt-2 text-ink-mute">
          /blog/{post.slug}
        </p>
      </header>

      <PostForm initial={initial} categories={categories} mode="edit" />
    </div>
  );
}
