import { ArrowLeft, MessageCircle, Pin, Star } from "lucide-react"
import Image from "next/image"
import Link from "next/link"
import { notFound } from "next/navigation"
import { PostComments } from "@/components/post-comments"
import { PostLikeButton } from "@/components/post-like-button"
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
import { Badge } from "@/components/ui/badge"
import { Card, CardContent, CardHeader } from "@/components/ui/card"
import { Separator } from "@/components/ui/separator"
import { getOrderById, getPlayerById, getPostById, listCommentsByPost } from "@/lib/api"
import { roleLabels } from "@/lib/constants"

export default async function PostDetailPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params
  const post = getPostById(id)
  if (!post) notFound()

  const comments = listCommentsByPost(id)
  const linkedOrder = post.linkedOrderId ? getOrderById(post.linkedOrderId) : null
  const linkedPlayer = linkedOrder ? getPlayerById(linkedOrder.playerId) : null

  return (
    <div className="container mx-auto py-8 px-4 max-w-2xl">
      <Link
        href="/community"
        className="inline-flex items-center gap-1 text-sm text-muted-foreground hover:text-foreground mb-4"
      >
        <ArrowLeft className="h-4 w-4" />
        返回社区
      </Link>

      <Card>
        <CardHeader className="pb-3">
          <div className="flex items-center gap-3">
            <Avatar className="h-10 w-10">
              <AvatarImage src={post.author.avatar} />
              <AvatarFallback>{post.author.nickname[0]}</AvatarFallback>
            </Avatar>
            <div className="flex-1 min-w-0">
              <div className="flex items-center gap-2">
                <span className="font-medium">{post.author.nickname}</span>
                <Badge variant="outline" className="text-[10px] px-1.5 py-0">
                  {roleLabels[post.authorRole]}
                </Badge>
                {post.pinned && <Pin className="h-3 w-3 text-muted-foreground" />}
              </div>
              <span className="text-xs text-muted-foreground">
                {new Date(post.createdAt).toLocaleString("zh-CN")}
              </span>
            </div>
          </div>
        </CardHeader>
        <CardContent className="space-y-4">
          <h1 className="text-xl font-bold">{post.title}</h1>
          <p className="text-sm leading-relaxed whitespace-pre-wrap">{post.content}</p>

          {post.images.length > 0 && (
            <div className="flex gap-2 flex-wrap">
              {post.images.map((img) => (
                <div key={img} className="relative h-40 w-40 rounded-md overflow-hidden bg-muted">
                  <Image src={img} alt="帖子图片" fill className="object-cover" />
                </div>
              ))}
            </div>
          )}

          {linkedOrder && (
            <Link href={`/order/${linkedOrder.id}`}>
              <div className="rounded-lg border bg-muted/30 p-3 text-sm hover:bg-muted/50 transition-colors">
                <div className="flex items-center gap-2 mb-1">
                  <Star className="h-3.5 w-3.5 text-yellow-500" />
                  <span className="font-medium">关联订单</span>
                </div>
                <p className="text-muted-foreground text-xs">
                  {linkedOrder.service.gameName} · {linkedOrder.service.title} · 评分{" "}
                  {linkedPlayer?.rating ?? "--"}
                </p>
              </div>
            </Link>
          )}

          {post.tags.length > 0 && (
            <div className="flex flex-wrap gap-1">
              {post.tags.map((tag) => (
                <Badge key={tag} variant="secondary" className="text-xs">
                  {tag}
                </Badge>
              ))}
            </div>
          )}

          <div className="flex items-center gap-4 text-sm text-muted-foreground pt-2">
            <PostLikeButton initialLiked={post.liked} initialCount={post.likeCount} />
            <span className="flex items-center gap-1">
              <MessageCircle className="h-4 w-4" />
              {post.commentCount}
            </span>
          </div>
        </CardContent>
      </Card>

      <Separator className="my-6" />

      <PostComments initialComments={comments} postId={id} />
    </div>
  )
}
