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

const prisma = new PrismaClient()

export async function GET() {
  try {
    // 获取在已发布的公开提示词中使用的标签
    const tags = await prisma.promptTag.findMany({
      where: {
        prompts: {
          some: {
            permissions: 'public',
            visibility: 'published',
          }
        }
      },
      include: {
        _count: {
          select: {
            prompts: {
              where: {
                permissions: 'public',
                visibility: 'published',
              }
            }
          }
        }
      },
      orderBy: {
        prompts: {
          _count: 'desc'
        }
      },
      take: 20 // 最多返回20个热门标签
    })

    return NextResponse.json({ tags })
  } catch (error) {
    console.error('Error fetching plaza tags:', error)
    return NextResponse.json(
      { error: 'Failed to fetch tags' },
      { status: 500 }
    )
  }
}