import { db } from "@/lib/db";
import { posts, users } from "@/lib/db/schema";
import { eq } from "drizzle-orm";
import { notFound } from "next/navigation";
import Link from "next/link";
import Comments from "@/components/Comments";

export const dynamic = "force-dynamic";

export default async function SinglePostPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const row = (await db.select({ post: posts, user: users }).from(posts).leftJoin(users, eq(posts.userId, users.id)).where(eq(posts.id, id)))[0];
  if (!row) return notFound();

  const { post, user } = row;

  return (
    <div className="max-w-2xl mx-auto">
      <Link href="/community" className="text-red-500 text-sm mb-4 inline-block">← Back to community</Link>
      <div className="card p-4">
        <div className="flex items-center gap-2 mb-2">
          <div className="w-8 h-8 rounded-full bg-neutral-700 flex items-center justify-center text-xs font-bold shrink-0">
            {user?.name?.[0]?.toUpperCase() || "?"}
          </div>
          <Link href={`/profile/${post.userId}`} className="font-semibold text-sm hover:text-red-400">{user?.name || "Unknown"}</Link>
        </div>
        <p className="text-sm text-neutral-200 whitespace-pre-wrap break-words mb-3">{post.content}</p>
        {post.attachmentType === "image" && post.attachmentUrl && (
          <img src={post.attachmentUrl} className="rounded-lg max-h-96 w-full object-cover mb-3" />
        )}
        <div className="flex gap-3 text-xs text-neutral-400 mb-3">
          <span>🙏 {post.prayedCount}</span>
          <span>❤️ {post.heartCount}</span>
          <span>✨ {post.sparkleCount}</span>
          <span>👍 {post.likeCount}</span>
        </div>
        <Comments postId={post.id} />
      </div>
    </div>
  );
}
