import { NextRequest, NextResponse } from 'next/server'; import { ProjectService, EvaluationService, ProjectFileService } from '@/lib/services/database'; export async function GET(request: NextRequest) { try { console.log('πŸ“Š ι–‹ε§‹η²ε–ζ­·ε²θ¨˜ιŒ„ζ•Έζ“š...'); // η²ε–ζ‰€ζœ‰ε°ˆζ‘ˆεŠε…Άθ©•ε―©η΅ζžœ const projects = await ProjectService.getAll(); console.log(`πŸ“‹ ζ‰Ύεˆ° ${projects.length} ε€‹ε°ˆζ‘ˆ`); const historyData = []; for (const project of projects) { // η²ε–θ©²ε°ˆζ‘ˆηš„ζœ€ζ–°θ©•ε―©η΅ζžœ const latestEvaluation = await EvaluationService.findByProjectId(project.id); // η²ε–θ©²ε°ˆζ‘ˆηš„ζ–‡δ»ΆδΏ‘ζ― const projectFiles = await ProjectFileService.findByProjectId(project.id); // εˆ€ζ–·ζ–‡δ»Άι‘žεž‹ let fileType = 'Unknown'; if (projectFiles.length > 0) { const fileTypeFromDB = projectFiles[0].file_type; if (fileTypeFromDB) { // ζ Ήζ“š file_type εˆ€ζ–·ζ–‡δ»Άι‘žεž‹ if (fileTypeFromDB.toLowerCase().includes('ppt') || fileTypeFromDB.toLowerCase().includes('powerpoint')) { fileType = 'PPT'; } else if (fileTypeFromDB.toLowerCase().includes('mp4') || fileTypeFromDB.toLowerCase().includes('avi') || fileTypeFromDB.toLowerCase().includes('mov')) { fileType = 'Video'; } else if (fileTypeFromDB.toLowerCase().includes('pdf')) { fileType = 'PDF'; } else if (fileTypeFromDB.toLowerCase().includes('html') || fileTypeFromDB.toLowerCase().includes('htm')) { fileType = 'Website'; } else { // ε¦‚ζžœη„‘ζ³•εΎž file_type εˆ€ζ–·οΌŒε˜—θ©¦εΎžζͺ”εεˆ€ζ–· const fileName = projectFiles[0].original_name.toLowerCase(); if (fileName.includes('.ppt') || fileName.includes('.pptx')) { fileType = 'PPT'; } else if (fileName.includes('.mp4') || fileName.includes('.avi') || fileName.includes('.mov')) { fileType = 'Video'; } else if (fileName.includes('.pdf')) { fileType = 'PDF'; } else if (fileName.includes('.html') || fileName.includes('.htm')) { fileType = 'Website'; } else { fileType = fileTypeFromDB.toUpperCase(); } } } } else { // ε¦‚ζžœζ²’ζœ‰ζ–‡δ»Άθ¨˜ιŒ„οΌŒε˜—θ©¦εΎžε°ˆζ‘ˆζ¨™ι‘Œεˆ€ζ–· if (project.title.includes('PPT') || project.title.includes('η°‘ε ±')) { fileType = 'PPT'; } else if (project.title.includes('ηΆ²η«™') || project.title.includes('Website')) { fileType = 'Website'; } else if (project.title.includes('影片') || project.title.includes('Video')) { fileType = 'Video'; } } // ζ ΌεΌεŒ–ζ—₯期 const formatDate = (date: Date) => { return new Date(date).toISOString().split('T')[0]; }; historyData.push({ id: project.id.toString(), title: project.title, type: fileType, score: latestEvaluation?.overall_score || 0, grade: latestEvaluation?.grade || '-', date: formatDate(project.created_at), status: project.status === 'completed' ? 'completed' : 'processing', evaluation_id: latestEvaluation?.id || null, description: project.description || '', created_at: project.created_at, updated_at: project.updated_at }); } // ζŒ‰ε‰΅ε»Ίζ™‚ι–“ε€’εΊζŽ’εˆ— historyData.sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime()); console.log(`βœ… ζˆεŠŸη²ε– ${historyData.length} ζ’ζ­·ε²θ¨˜ιŒ„`); return NextResponse.json({ success: true, data: historyData }); } catch (error) { console.error('❌ η²ε–ζ­·ε²θ¨˜ιŒ„ε€±ζ•—:', error); return NextResponse.json( { success: false, error: 'η²ε–ζ­·ε²θ¨˜ιŒ„ε€±ζ•—' }, { status: 500 } ); } }