import { NextRequest, NextResponse } from "next/server"; import { db } from "@/lib/db"; import ZAI from "z-ai-web-dev-sdk"; export async function POST(request: NextRequest) { try { const body = await request.json(); const { code, repoId, projectId } = body; if (!code) { return NextResponse.json({ success: false, error: "Codigo requerido" }, { status: 400 }); } const zai = await ZAI.create(); const completion = await zai.chat.completions.create({ messages: [ { role: "system", content: "Eres un analista de codigo. Responde en JSON con {summary, issues:[], suggestions:[]}" }, { role: "user", content: "Analiza: " + code.substring(0, 3000) } ] }); let analysis: { summary?: string; issues?: string[]; suggestions?: string[] } = {}; try { const match = completion.choices[0]?.message?.content?.match(/\{[\s\S]*\}/); if (match) analysis = JSON.parse(match[0]); } catch {} const result = await db.analysis.create({ data: { type: "code", result: JSON.stringify(analysis), summary: analysis.summary || "Analisis completado", repoId: repoId || null, projectId: projectId || null } }); return NextResponse.json({ success: true, analysis: result }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } } export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); const projectId = searchParams.get("projectId"); const where: Record = {}; if (projectId) where.projectId = projectId; const analyses = await db.analysis.findMany({ where, orderBy: { createdAt: "desc" }, take: 20 }); return NextResponse.json({ success: true, analyses }); } catch { return NextResponse.json({ success: false, error: "Error" }, { status: 500 }); } }