import { NextRequest, NextResponse } from 'next/server'; import { GeminiService } from '@/lib/services/gemini'; import { CriteriaItemService } from '@/lib/services/database'; export async function POST(request: NextRequest) { try { const formData = await request.formData(); const projectTitle = formData.get('projectTitle') as string; const projectDescription = formData.get('projectDescription') as string; const file = formData.get('file') as File; const websiteUrl = formData.get('websiteUrl') as string; console.log('🚀 開始處理評審請求...'); console.log('📝 專案標題:', projectTitle); console.log('📋 專案描述:', projectDescription); console.log('📁 上傳文件:', file ? file.name : '無'); console.log('🌐 網站連結:', websiteUrl || '無'); // 驗證必填欄位 if (!projectTitle?.trim()) { return NextResponse.json( { success: false, error: '請填寫專案標題' }, { status: 400 } ); } if (!file && !websiteUrl?.trim()) { return NextResponse.json( { success: false, error: '請上傳文件或提供網站連結' }, { status: 400 } ); } // 獲取評分標準 console.log('📊 載入評分標準...'); const templates = await CriteriaItemService.getAllTemplates(); console.log('📊 載入的模板:', templates); if (!templates || templates.length === 0) { return NextResponse.json( { success: false, error: '未找到評分標準,請先設定評分標準' }, { status: 400 } ); } const criteria = templates[0].items || []; console.log('📊 評分項目:', criteria); console.log('✅ 評分標準載入完成,共', criteria.length, '個項目'); let content = ''; if (file) { // 處理文件上傳 console.log('📄 開始處理上傳文件...'); // 檢查文件類型 const allowedTypes = [ 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'video/mp4', 'video/avi', 'video/quicktime', 'application/pdf', 'text/plain' // 添加純文字格式用於測試 ]; if (!allowedTypes.includes(file.type)) { console.log('❌ 不支援的文件格式:', file.type); return NextResponse.json( { success: false, error: `不支援的文件格式: ${file.type}` }, { status: 400 } ); } // 檢查文件大小 (100MB) const maxSize = 100 * 1024 * 1024; if (file.size > maxSize) { return NextResponse.json( { success: false, error: '文件大小超過 100MB 限制' }, { status: 400 } ); } // 提取內容 content = await GeminiService.extractPPTContent(file); } else if (websiteUrl) { // 處理網站連結 console.log('🌐 開始處理網站連結...'); content = `網站內容分析: ${websiteUrl} 由於目前是簡化版本,無法直接抓取網站內容。 實際應用中應該使用網頁抓取技術來獲取網站內容進行分析。 專案描述: ${projectDescription || '無'}`; } // 使用 Gemini AI 進行評分 console.log('🤖 開始 AI 評分...'); const evaluation = await GeminiService.analyzePresentation( content, projectTitle, projectDescription || '', criteria ); // 儲存評審結果到資料庫(可選) // TODO: 實作結果儲存功能 console.log('🎉 評審完成!'); console.log('📊 最終評分結果:'); console.log(' 總分:', evaluation.totalScore, '/', evaluation.maxTotalScore); console.log(' 各項目評分:'); evaluation.results.forEach(result => { console.log(` - ${result.criteriaName}: ${result.score}/${result.maxScore}`); }); return NextResponse.json({ success: true, data: { evaluation, projectTitle, projectDescription, fileInfo: file ? { name: file.name, size: file.size, type: file.type } : null, websiteUrl: websiteUrl || null } }); } catch (error) { console.error('❌ 評審處理失敗:', error); return NextResponse.json( { success: false, error: '評審處理失敗,請稍後再試' }, { status: 500 } ); } }