import { NextRequest, NextResponse } from 'next/server' import { AppService } from '@/lib/services/database-service' const appService = new AppService() // 獲取應用的評論列表 export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { id: appId } = await params const { searchParams } = new URL(request.url) const limit = parseInt(searchParams.get('limit') || '5') // 預設5筆 const offset = parseInt(searchParams.get('offset') || '0') const page = parseInt(searchParams.get('page') || '1') const reviews = await appService.getAppReviews(appId, limit, offset) const totalReviews = await appService.getAppReviewCount(appId) const totalPages = Math.ceil(totalReviews / limit) return NextResponse.json({ success: true, data: { reviews, pagination: { page, limit, total: totalReviews, totalPages, hasNext: page < totalPages, hasPrev: page > 1 } } }) } catch (error) { console.error('獲取應用評論錯誤:', error) return NextResponse.json( { success: false, error: '獲取應用評論時發生錯誤' }, { status: 500 } ) } } // 創建新的評論 export async function POST( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { id: appId } = await params const { userId, rating, comment, reviewId } = await request.json() if (!userId || !rating || !comment) { return NextResponse.json( { success: false, error: '缺少必要參數' }, { status: 400 } ) } // 檢查評分範圍 if (rating < 1 || rating > 5) { return NextResponse.json( { success: false, error: '評分必須在 1-5 之間' }, { status: 400 } ) } let resultReviewId: string | null = null if (reviewId) { // 更新現有評論 resultReviewId = await appService.updateAppReview(reviewId, appId, userId, rating, comment) } else { // 創建新評論 resultReviewId = await appService.createAppReview(appId, userId, rating, comment) } if (resultReviewId) { // 獲取更新後的評論列表 const reviews = await appService.getAppReviews(appId, 10, 0) return NextResponse.json({ success: true, data: { reviewId: resultReviewId, reviews } }) } else { return NextResponse.json( { success: false, error: reviewId ? '更新評論失敗' : '創建評論失敗' }, { status: 500 } ) } } catch (error) { console.error('處理應用評論錯誤:', error) return NextResponse.json( { success: false, error: '處理應用評論時發生錯誤' }, { status: 500 } ) } }