File size: 23,190 Bytes
cb69733
 
 
 
 
f7b8e4c
cb69733
f7b8e4c
cb69733
484977e
f7b8e4c
cb69733
 
f7b8e4c
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb69733
f7b8e4c
 
 
 
 
 
484977e
 
f7b8e4c
 
 
cb69733
 
f7b8e4c
cb69733
f7b8e4c
cb69733
f7b8e4c
 
 
484977e
f7b8e4c
 
 
484977e
cb69733
 
f7b8e4c
 
 
 
 
 
 
cb69733
f7b8e4c
 
 
 
 
 
 
 
cb69733
f7b8e4c
 
 
 
 
 
 
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484977e
f7b8e4c
 
 
 
 
cb69733
f7b8e4c
 
 
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb69733
484977e
 
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484977e
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb69733
 
f7b8e4c
 
 
 
 
cb69733
f7b8e4c
 
 
 
 
 
 
 
cb69733
f7b8e4c
 
 
cb69733
 
 
f7b8e4c
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
484977e
cb69733
 
 
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
484977e
f7b8e4c
 
 
 
 
484977e
 
cb69733
 
f7b8e4c
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
484977e
 
cb69733
 
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484977e
cb69733
f7b8e4c
 
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb69733
 
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb69733
 
f7b8e4c
 
cb69733
 
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484977e
f7b8e4c
 
cb69733
f7b8e4c
 
 
cb69733
f7b8e4c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb69733
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
<!DOCTYPE html>
<html lang="pt-BR">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Forensic Person Search System</title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
  <style>
    @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

    body {
      font-family: 'Inter', sans-serif;
      background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%);
    }

    .card-glow {
      box-shadow: 0 0 20px rgba(59, 130, 246, 0.1);
      transition: all 0.3s ease;
    }

    .card-glow:hover {
      transform: translateY(-5px);
      box-shadow: 0 10px 30px rgba(59, 130, 246, 0.2);
    }

    .gradient-border {
      background: linear-gradient(45deg, #3b82f6, #8b5cf6);
      padding: 1px;
      border-radius: 0.75rem;
    }

    .gradient-border-content {
      background: #0f172a;
      border-radius: 0.625rem;
    }

    .timeline-item::before {
      content: '';
      position: absolute;
      left: 10px;
      top: 0;
      bottom: 0;
      width: 2px;
      background: linear-gradient(to bottom, #3b82f6, #8b5cf6);
    }

    .timeline-dot {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background: linear-gradient(45deg, #3b82f6, #8b5cf6);
      position: absolute;
      left: 5px;
    }

    .loading-spinner {
      width: 40px;
      height: 40px;
      border: 4px solid rgba(59, 130, 246, 0.2);
      border-top-color: #3b82f6;
      border-radius: 50%;
      animation: spin 1s linear infinite;
    }

    @keyframes spin {
      to { transform: rotate(360deg); }
    }

    .network-graph {
      background: radial-gradient(circle at center, rgba(59, 130, 246, 0.1) 0%, transparent 70%);
    }

    .risk-meter {
      background: linear-gradient(to right, #ef4444, #f59e0b, #10b981);
    }
  </style>
</head>
<body class="min-h-screen p-4 md:p-6">
  <div class="max-w-7xl mx-auto">
    <!-- Header -->
    <header class="flex justify-between items-center mb-8">
      <div class="flex items-center space-x-3">
        <div class="w-10 h-10 bg-gradient-to-r from-blue-500 to-purple-600 rounded-lg flex items-center justify-center">
          <i class="fas fa-search text-white text-lg"></i>
        </div>
        <h1 class="text-2xl font-bold text-white">Forensic Search System</h1>
      </div>
      <div class="text-sm text-slate-400">
        <a href="https://huggingface.co/spaces/akhaliq/anycoder" class="hover:text-blue-400 transition-colors">
          Built with anycoder
        </a>
      </div>
    </header>

    <!-- Main Content -->
    <div id="app" class="space-y-6">
      <!-- Status Bar -->
      <div id="status" class="text-xs text-slate-500 mb-4 flex items-center space-x-2">
        <span class="w-2 h-2 rounded-full bg-yellow-500 animate-pulse"></span>
        <span>Aguardando Autenticação...</span>
      </div>

      <!-- Search Form -->
      <div class="gradient-border">
        <div class="gradient-border-content p-6">
          <form id="searchForm" class="space-y-4">
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
              <div>
                <label class="block text-sm font-medium text-slate-300 mb-1">CPF</label>
                <input type="text" id="cpf" placeholder="000.000.000-00"
                       class="w-full px-4 py-2 bg-slate-800 border border-slate-700 rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-blue-500">
              </div>
              <div>
                <label class="block text-sm font-medium text-slate-300 mb-1">Rede Social</label>
                <select id="socialNetwork" class="w-full px-4 py-2 bg-slate-800 border border-slate-700 rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-blue-500">
                  <option value="facebook">Facebook</option>
                  <option value="twitter">Twitter/X</option>
                  <option value="instagram">Instagram</option>
                  <option value="linkedin">LinkedIn</option>
                  <option value="tiktok">TikTok</option>
                </select>
              </div>
              <div>
                <label class="block text-sm font-medium text-slate-300 mb-1">Perfil</label>
                <input type="text" id="profileUrl" placeholder="https://social.com/username"
                       class="w-full px-4 py-2 bg-slate-800 border border-slate-700 rounded-lg text-white focus:outline-none focus:ring-2 focus:ring-blue-500">
              </div>
            </div>
            <div class="flex justify-end">
              <button id="btnSearch" type="submit" class="bg-gradient-to-r from-blue-600 to-purple-600 text-white px-6 py-2 rounded-lg font-medium hover:opacity-90 transition-opacity disabled:opacity-50 disabled:cursor-not-allowed">
                <i class="fas fa-search mr-2"></i>
                Iniciar Pesquisa Forense
              </button>
            </div>
          </form>
        </div>
      </div>

      <!-- Results Dashboard -->
      <div id="resultsDashboard" class="hidden space-y-6">
        <!-- Summary Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
          <div class="card-glow bg-slate-800 p-6 rounded-xl">
            <div class="flex items-center space-x-3 mb-4">
              <div class="w-12 h-12 bg-blue-600 rounded-full flex items-center justify-center">
                <i class="fas fa-user text-white"></i>
              </div>
              <div>
                <h3 class="text-lg font-semibold text-white">Perfil Analisado</h3>
                <p id="profileName" class="text-slate-300">-</p>
              </div>
            </div>
            <div class="space-y-2">
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Postagens Analisadas</span>
                <span id="postsCount" class="text-lg font-bold text-white">0</span>
              </div>
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Período Coberto</span>
                <span id="periodCovered" class="text-lg font-bold text-white">-</span>
              </div>
            </div>
          </div>

          <div class="card-glow bg-slate-800 p-6 rounded-xl">
            <div class="flex items-center space-x-3 mb-4">
              <div class="w-12 h-12 bg-purple-600 rounded-full flex items-center justify-center">
                <i class="fas fa-chart-line text-white"></i>
              </div>
              <div>
                <h3 class="text-lg font-semibold text-white">Atividade</h3>
                <p class="text-slate-300">Métricas de Engajamento</p>
              </div>
            </div>
            <div class="space-y-2">
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Média Diária</span>
                <span id="dailyAvg" class="text-lg font-bold text-white">0</span>
              </div>
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Pico de Atividade</span>
                <span id="peakActivity" class="text-lg font-bold text-white">-</span>
              </div>
            </div>
          </div>

          <div class="card-glow bg-slate-800 p-6 rounded-xl">
            <div class="flex items-center space-x-3 mb-4">
              <div class="w-12 h-12 bg-red-600 rounded-full flex items-center justify-center">
                <i class="fas fa-exclamation-triangle text-white"></i>
              </div>
              <div>
                <h3 class="text-lg font-semibold text-white">Riscos Detectados</h3>
                <p class="text-slate-300">Avaliação de Perigo</p>
              </div>
            </div>
            <div class="space-y-2">
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Nível de Risco</span>
                <span id="riskLevel" class="text-lg font-bold text-red-400">Baixo</span>
              </div>
              <div class="w-full h-2 bg-slate-700 rounded-full mt-2">
                <div id="riskMeter" class="h-full rounded-full risk-meter" style="width: 20%"></div>
              </div>
            </div>
          </div>

          <div class="card-glow bg-slate-800 p-6 rounded-xl">
            <div class="flex items-center space-x-3 mb-4">
              <div class="w-12 h-12 bg-green-600 rounded-full flex items-center justify-center">
                <i class="fas fa-network-wired text-white"></i>
              </div>
              <div>
                <h3 class="text-lg font-semibold text-white">Conexões</h3>
                <p class="text-slate-300">Rede Social</p>
              </div>
            </div>
            <div class="space-y-2">
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Seguidores</span>
                <span id="followers" class="text-lg font-bold text-white">0</span>
              </div>
              <div class="flex justify-between">
                <span class="text-sm text-slate-400">Seguindo</span>
                <span id="following" class="text-lg font-bold text-white">0</span>
              </div>
            </div>
          </div>
        </div>

        <!-- Detailed Analysis -->
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
          <!-- Timeline -->
          <div class="lg:col-span-2 space-y-4">
            <div class="gradient-border">
              <div class="gradient-border-content p-6">
                <h2 class="text-xl font-bold text-white mb-4 flex items-center">
                  <i class="fas fa-clock mr-2"></i>
                  Linha do Tempo
                </h2>
                <div id="timeline" class="space-y-6">
                  <!-- Timeline items will be added here -->
                </div>
              </div>
            </div>

            <!-- Content Analysis -->
            <div class="gradient-border">
              <div class="gradient-border-content p-6">
                <h2 class="text-xl font-bold text-white mb-4 flex items-center">
                  <i class="fas fa-chart-pie mr-2"></i>
                  Análise de Conteúdo
                </h2>
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                  <div>
                    <h3 class="text-lg font-semibold text-white mb-2">Tópicos Principais</h3>
                    <div id="topicsChart" class="h-48 bg-slate-800 rounded-lg flex items-center justify-center">
                      <span class="text-slate-400">Gráfico de tópicos</span>
                    </div>
                  </div>
                  <div>
                    <h3 class="text-lg font-semibold text-white mb-2">Sentimento</h3>
                    <div id="sentimentChart" class="h-48 bg-slate-800 rounded-lg flex items-center justify-center">
                      <span class="text-slate-400">Gráfico de sentimento</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- Network Graph -->
          <div class="space-y-4">
            <div class="gradient-border">
              <div class="gradient-border-content p-6">
                <h2 class="text-xl font-bold text-white mb-4 flex items-center">
                  <i class="fas fa-project-diagram mr-2"></i>
                  Rede de Conexões
                </h2>
                <div id="networkGraph" class="h-64 bg-slate-800 rounded-lg network-graph flex items-center justify-center">
                  <span class="text-slate-400">Visualização de rede</span>
                </div>
              </div>
            </div>

            <!-- Risk Assessment -->
            <div class="gradient-border">
              <div class="gradient-border-content p-6">
                <h2 class="text-xl font-bold text-white mb-4 flex items-center">
                  <i class="fas fa-shield-alt mr-2"></i>
                  Avaliação de Risco
                </h2>
                <div class="space-y-4">
                  <div>
                    <h3 class="text-lg font-semibold text-white mb-2">Alertas</h3>
                    <div id="riskAlerts" class="space-y-2">
                      <!-- Risk alerts will be added here -->
                    </div>
                  </div>
                  <div>
                    <h3 class="text-lg font-semibold text-white mb-2">Recomendações</h3>
                    <div id="recommendations" class="space-y-2">
                      <!-- Recommendations will be added here -->
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- Loading State -->
      <div id="loadingState" class="hidden text-center py-12">
        <div class="loading-spinner mx-auto mb-4"></div>
        <h3 class="text-xl font-semibold text-white mb-2">Processando Análise Forense</h3>
        <p class="text-slate-400">Estamos coletando e analisando dados dos últimos 5 anos...</p>
        <div class="mt-4 text-sm text-slate-500">
          <p>Esta operação pode levar alguns minutos dependendo da quantidade de dados.</p>
          <p>Por favor, não feche esta janela.</p>
        </div>
      </div>

      <!-- Error State -->
      <div id="errorState" class="hidden bg-red-900/20 border border-red-500 rounded-lg p-4 text-red-300">
        <div class="flex items-start space-x-3">
          <i class="fas fa-exclamation-circle text-red-400 mt-1"></i>
          <div>
            <h3 class="font-semibold text-white mb-2">Erro na Análise</h3>
            <p id="errorMessage">Ocorreu um erro ao processar sua solicitação.</p>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script>
    // Configuration
    const PLUGIN_ID = "forensic-search-system-v1";
    let authToken = null;
    let currentAnalysis = null;

    // DOM Elements
    const statusElement = document.getElementById('status');
    const searchForm = document.getElementById('searchForm');
    const btnSearch = document.getElementById('btnSearch');
    const resultsDashboard = document.getElementById('resultsDashboard');
    const loadingState = document.getElementById('loadingState');
    const errorState = document.getElementById('errorState');

    // 1. Authentication Handshake
    window.addEventListener('message', (event) => {
      if (event.data.type === 'AUTH_TOKEN') {
        authToken = event.data.token;
        statusElement.innerHTML = `
          <span class="w-2 h-2 rounded-full bg-green-500"></span>
          <span>Autenticado com sucesso</span>
        `;
        btnSearch.disabled = false;
      }
    });

    // 2. Form Submission
    searchForm.addEventListener('submit', async (e) => {
      e.preventDefault();

      if (!authToken) {
        showError("Token de autenticação não disponível");
        return;
      }

      // Get form values
      const cpf = document.getElementById('cpf').value;
      const socialNetwork = document.getElementById('socialNetwork').value;
      const profileUrl = document.getElementById('profileUrl').value;

      if (!cpf || !profileUrl) {
        showError("Por favor, preencha todos os campos obrigatórios");
        return;
      }

      // Show loading state
      resultsDashboard.classList.add('hidden');
      loadingState.classList.remove('hidden');
      errorState.classList.add('hidden');

      try {
        // Call AI Gateway
        const analysis = await callAIGateway(cpf, socialNetwork, profileUrl);
        currentAnalysis = analysis;
        renderResults(analysis);
      } catch (error) {
        showError(error.message || "Falha ao processar a análise");
      }
    });

    // 3. AI Gateway Call
    async function callAIGateway(cpf, socialNetwork, profileUrl) {
      try {
        const response = await fetch('/api/client/plugin/ai', {
          method: 'POST',
          headers: {
            'Authorization': `Bearer ${authToken}`,
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({
            plugin_id: PLUGIN_ID,
            user_prompt: `Analise forensicamente o perfil ${profileUrl} na rede social ${socialNetwork} associado ao CPF ${cpf}. Colete dados dos últimos 5 anos e gere um relatório detalhado com:
            1. Informações básicas do perfil
            2. Análise de atividade (postagens, engajamento)
            3. Linha do tempo de eventos significativos
            4. Análise de conteúdo (tópicos, sentimento)
            5. Rede de conexões
            6. Avaliação de risco
            7. Alertas e recomendações

            Formate a resposta como JSON com a seguinte estrutura:
            {
              "profile": {...},
              "activity": {...},
              "timeline": [...],
              "contentAnalysis": {...},
              "network": {...},
              "riskAssessment": {...}
            }`
          })
        });

        if (!response.ok) {
          throw new Error(`Erro na API: ${response.status}`);
        }

        const data = await response.json();
        return JSON.parse(data.response); // Assuming the AI returns JSON in the response field
      } catch (error) {
        console.error("Error calling AI Gateway:", error);
        throw error;
      }
    }

    // 4. Render Results
    function renderResults(analysis) {
      // Hide loading state
      loadingState.classList.add('hidden');

      // Populate summary cards
      document.getElementById('profileName').textContent = analysis.profile.name || '-';
      document.getElementById('postsCount').textContent = analysis.activity.postsCount || '0';
      document.getElementById('periodCovered').textContent = analysis.activity.period || '-';
      document.getElementById('dailyAvg').textContent = analysis.activity.dailyAverage || '0';
      document.getElementById('peakActivity').textContent = analysis.activity.peakDate || '-';

      // Risk assessment
      const riskLevel = analysis.riskAssessment.level || 'Baixo';
      const riskPercentage = getRiskPercentage(riskLevel);
      document.getElementById('riskLevel').textContent = riskLevel;
      document.getElementById('riskMeter').style.width = `${riskPercentage}%`;

      // Network
      document.getElementById('followers').textContent = analysis.network.followers || '0';
      document.getElementById('following').textContent = analysis.network.following || '0';

      // Render timeline
      renderTimeline(analysis.timeline);

      // Render risk alerts and recommendations
      renderAlerts(analysis.riskAssessment.alerts);
      renderRecommendations(analysis.riskAssessment.recommendations);

      // Show results
      resultsDashboard.classList.remove('hidden');
    }

    // 5. Helper Functions
    function getRiskPercentage(level) {
      const levels = {
        'Baixo': 20,
        'Médio': 50,
        'Alto': 80,
        'Crítico': 100
      };
      return levels[level] || 20;
    }

    function renderTimeline(items) {
      const timelineElement = document.getElementById('timeline');
      timelineElement.innerHTML = '';

      if (!items || items.length === 0) {
        timelineElement.innerHTML = '<p class="text-slate-400">Nenhum evento significativo encontrado.</p>';
        return;
      }

      items.forEach(item => {
        const timelineItem = document.createElement('div');
        timelineItem.className = 'relative pl-8 pb-4';

        timelineItem.innerHTML = `
          <div class="timeline-dot"></div>
          <div class="bg-slate-800 p-4 rounded-lg border border-slate-700">
            <div class="flex justify-between items-start mb-2">
              <h4 class="font-semibold text-white">${item.title}</h4>
              <span class="text-xs text-slate-400">${item.date}</span>
            </div>
            <p class="text-slate-300 mb-3">${item.description}</p>
            ${item.tags ? `
              <div class="flex flex-wrap gap-2">
                ${item.tags.map(tag => `
                  <span class="px-2 py-1 bg-slate-700 text-xs rounded-full text-slate-300">${tag}</span>
                `).join('')}
              </div>
            ` : ''}
          </div>
        `;

        timelineElement.appendChild(timelineItem);
      });
    }

    function renderAlerts(alerts) {
      const alertsElement = document.getElementById('riskAlerts');
      alertsElement.innerHTML = '';

      if (!alerts || alerts.length === 0) {
        alertsElement.innerHTML = '<p class="text-slate-400 text-sm">Nenhum alerta encontrado.</p>';
        return;
      }

      alerts.forEach(alert => {
        const alertElement = document.createElement('div');
        alertElement.className = 'bg-red-900/30 border border-red-500 rounded-lg p-3';

        alertElement.innerHTML = `
          <div class="flex items-start space-x-3">
            <i class="fas fa-exclamation-triangle text-red-400 mt-1"></i>
            <div>
              <h4 class="font-semibold text-white">${alert.title}</h4>
              <p class="text-red-300 text-sm">${alert.description}</p>
              ${alert.severity ? `
                <span class="text-xs text-red-400 mt-1 block">Severidade: ${alert.severity}</span>
              ` : ''}
            </div>
          </div>
        `;

        alertsElement.appendChild(alertElement);
      });
    }

    function renderRecommendations(recommendations) {
      const recElement = document.getElementById('recommendations');
      recElement.innerHTML = '';

      if (!recommendations || recommendations.length === 0) {
        recElement.innerHTML = '<p class="text-slate-400 text-sm">Nenhuma recomendação.</p>';
        return;
      }

      recommendations.forEach(rec => {
        const recElementItem = document.createElement('div');
        recElementItem.className = 'bg-blue-900/30 border border-blue-500 rounded-lg p-3';

        recElementItem.innerHTML = `
          <div class="flex items-start space-x-3">
            <i class="fas fa-lightbulb text-blue-400 mt-1"></i>
            <div>
              <h4 class="font-semibold text-white">${rec.title}</h4>
              <p class="text-blue-300 text-sm">${rec.description}</p>
            </div>
          </div>
        `;

        recElement.appendChild(recElementItem);
      });
    }

    function showError(message) {
      loadingState.classList.add('hidden');
      errorState.classList.remove('hidden');
      document.getElementById('errorMessage').textContent = message;
    }

    // Initialize
    document.addEventListener('DOMContentLoaded', () => {
      // Check if we're in an iframe
      if (window.self !== window.top) {
        // Send ready message to parent
        window.parent.postMessage({ type: 'PLUGIN_READY', pluginId: PLUGIN_ID }, '*');
      }
    });
  </script>
</body>
</html>