import { GoogleGenerativeAI } from "@google/generative-ai"; export default async function handler(req, res) { if (req.method !== 'POST') { return res.status(405).json({ message: 'Method not allowed' }); } try { const { query, locationType, startDate, endDate } = req.body; if (!query) { return res.status(400).json({ message: 'Query parameter is required' }); } // Get basic agent data (in a real app, this would come from your database) const basicData = getBasicAgentData(query, locationType); // Enhance with Gemini AI const enhancedData = await enhanceWithGemini(basicData, { location: query, locationType, startDate, endDate }); res.status(200).json(enhancedData); } catch (error) { console.error('API Error:', error); res.status(500).json({ message: 'Internal server error', error: error.message }); } } function getBasicAgentData(query, locationType) { // Mock data - replace with actual database query return [ { name: 'João Silva', position: 'Prefeito', location: `${query} - ${locationType}`, status: 'ativo', details: 'Gestor público com 10 anos de experiência.' }, { name: 'Maria Santos', position: 'Secretária de Educação', location: `${query} - ${locationType}`, status: 'ativo', details: 'Especialista em políticas educacionais.' } ]; } async function enhanceWithGemini(agents, context) { if (!process.env.NEXT_PUBLIC_GEMINI_API_KEY) { throw new Error('Gemini API key not configured'); } const genAI = new GoogleGenerativeAI(process.env.NEXT_PUBLIC_GEMINI_API_KEY); const model = genAI.getGenerativeModel({ model: "gemini-pro" }); const enhancedAgents = []; for (const agent of agents) { const prompt = ` Analyze this public agent for potential irregularities: Name: ${agent.name} Position: ${agent.position} Location: ${agent.location} Status: ${agent.status} Details: ${agent.details} Provide a brief forensic analysis and risk assessment. `; const result = await model.generateContent(prompt); const response = await result.response; const analysis = response.text(); enhancedAgents.push({ ...agent, analysis, riskScore: Math.floor(Math.random() * 100) // In real app, this would come from analysis }); } return enhancedAgents; }