import type { Metadata } from "next";
import Link from "next/link";
import { togglePublishAction } from "@/app/admin/actions";
import { DeletePostButton } from "@/components/admin/DeletePostButton";
import { getAllPosts } from "@/lib/cms";

export const metadata: Metadata = {
  title: "Journal posts",
  robots: { index: false, follow: false },
};

export default async function AdminPostsPage({
  searchParams,
}: {
  searchParams: Promise<{ saved?: string; deleted?: string }>;
}) {
  const [{ saved, deleted }, posts] = await Promise.all([
    searchParams,
    getAllPosts(),
  ]);

  return (
    <div className="space-y-8">
      <header className="flex flex-wrap items-end justify-between gap-4">
        <div>
          <p className="text-sm font-semibold uppercase tracking-[0.14em] text-coral">
            Content
          </p>
          <h1 className="display-2 mt-2">Journal posts</h1>
          <p className="mt-2 text-ink-soft">
            Write, edit and publish articles with cover images and SEO fields.
          </p>
        </div>
        <Link href="/admin/posts/new" className="btn btn-teal">
          New post
        </Link>
      </header>

      {saved ? (
        <p className="rounded-2xl border border-teal/30 bg-teal-soft px-5 py-3 text-sm font-medium text-teal-dark">
          Saved “{saved}”. {""}
          <Link href={`/blog/${saved}`} className="underline">
            View it on the site
          </Link>
        </p>
      ) : null}
      {deleted ? (
        <p className="rounded-2xl border border-coral/30 bg-coral-soft px-5 py-3 text-sm font-medium text-coral-dark">
          Post deleted.
        </p>
      ) : null}

      <div className="space-y-4">
        {posts.map((post) => (
          <article
            key={post.slug}
            className="card-soft flex flex-wrap items-center gap-x-6 gap-y-4 p-5"
          >
            <div className="min-w-0 flex-1">
              <div className="flex flex-wrap items-center gap-3">
                <Link
                  href={`/admin/posts/${post.slug}/edit`}
                  className="font-display text-xl font-semibold text-ink transition-colors hover:text-teal"
                >
                  {post.title}
                </Link>
                <span
                  className={`rounded-full px-2.5 py-1 text-[0.68rem] font-bold uppercase tracking-wide ${
                    post.published
                      ? "bg-teal-soft text-teal-dark"
                      : "bg-amber-soft text-amber-dark"
                  }`}
                >
                  {post.published ? "Published" : "Draft"}
                </span>
              </div>
              <p className="mt-1 text-xs text-ink-mute">
                /blog/{post.slug} · {post.category} · {post.date} · {post.readTime} read
              </p>
            </div>

            <div className="flex flex-wrap items-center gap-2">
              <form action={togglePublishAction}>
                <input type="hidden" name="slug" value={post.slug} />
                <button
                  type="submit"
                  className="rounded-full border border-linen px-3.5 py-1.5 text-xs font-semibold text-ink-soft transition-colors hover:border-teal hover:text-teal"
                >
                  {post.published ? "Unpublish" : "Publish"}
                </button>
              </form>
              <Link
                href={`/admin/posts/${post.slug}/edit`}
                className="rounded-full bg-teal px-3.5 py-1.5 text-xs font-semibold text-white transition-colors hover:bg-teal-dark"
              >
                Edit
              </Link>
              {post.published ? (
                <Link
                  href={`/blog/${post.slug}`}
                  className="rounded-full px-3.5 py-1.5 text-xs font-semibold text-ink-soft transition-colors hover:bg-mint"
                >
                  View
                </Link>
              ) : null}
              <DeletePostButton slug={post.slug} title={post.title} />
            </div>
          </article>
        ))}

        {posts.length === 0 ? (
          <div className="card-soft p-10 text-center">
            <p className="text-ink-soft">
              No journal posts yet. Create your first one to get started.
            </p>
            <Link href="/admin/posts/new" className="btn btn-teal mt-6">
              Write your first post
            </Link>
          </div>
        ) : null}
      </div>
    </div>
  );
}
