import { NextRequest, NextResponse } from 'next/server'
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const resolvedParams = await params
    const promptId = resolvedParams.id

    // 首先检查prompt是否存在且为公开发布状态
    const prompt = await prisma.prompt.findFirst({
      where: {
        id: promptId,
        permissions: 'public',
        visibility: 'published',
      }
    })

    if (!prompt) {
      return NextResponse.json(
        { error: 'Prompt not found' },
        { status: 404 }
      )
    }

    // TODO: 暂时禁用浏览计数功能，等待prompt_stats表创建
    // 将来可以启用以下代码：
    // await prisma.promptStats.upsert({
    //   where: { promptId: promptId },
    //   update: { viewCount: { increment: 1 } },
    //   create: { promptId: promptId, viewCount: 1 }
    // })

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error('Error updating view count:', error)
    return NextResponse.json(
      { error: 'Failed to update view count' },
      { status: 500 }
    )
  }
}