File size: 2,464 Bytes
9c04413
9385144
9c04413
9385144
 
 
 
 
9c04413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9385144
9c04413
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9385144
9c04413
 
9385144
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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;
}