<?php
echo "<h1>Teste Completo do Sistema</h1>";

// Testar API principal
echo "<h2>1. Teste da API Principal</h2>";
$url = 'https://sistemas.pinheiroempresas.com.br/controlefrota/backend-php/admin/api-monitoramento.php';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: $httpCode<br>";
if ($httpCode == 200) {
    $data = json_decode($response, true);
    if ($data && isset($data['solicitacoes'])) {
        echo "✅ API principal funcionando<br>";
        echo "Solicitações encontradas: " . count($data['solicitacoes']) . "<br>";
        
        // Verificar se tem placa
        if (count($data['solicitacoes']) > 0) {
            $primeira = $data['solicitacoes'][0];
            echo "Primeira solicitação - Placa: " . ($primeira['placa_veiculo'] ?? 'N/A') . "<br>";
        }
    }
}

// Testar API de filtros
echo "<h2>2. Teste da API de Filtros</h2>";
$url = $url . '?action=filtros';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: $httpCode<br>";
if ($httpCode == 200) {
    $data = json_decode($response, true);
    if ($data && isset($data['filtros'])) {
        echo "✅ API de filtros funcionando<br>";
        echo "Gestores: " . count($data['filtros']['gestores']) . "<br>";
        echo "Motoristas: " . count($data['filtros']['motoristas']) . "<br>";
        echo "Veículos: " . count($data['filtros']['veiculos']) . "<br>";
    }
}

// Testar API de alertas
echo "<h2>3. Teste da API de Alertas</h2>";
$url = 'https://sistemas.pinheiroempresas.com.br/controlefrota/backend-php/admin/api-monitoramento.php?action=check_alerts&last_check=2025-01-01T00:00:00.000Z';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP Code: $httpCode<br>";
if ($httpCode == 200) {
    $data = json_decode($response, true);
    if ($data && isset($data['novas_solicitacoes'])) {
        echo "✅ API de alertas funcionando<br>";
        echo "Novas solicitações: " . count($data['novas_solicitacoes']) . "<br>";
    }
}

echo "<hr>";
echo "<h2>Links de Teste</h2>";
echo "<a href='monitoramento-solicitacoes.html' target='_blank'>Página de Monitoramento</a><br>";
echo "<a href='api-monitoramento.php' target='_blank'>API Principal</a><br>";
echo "<a href='api-monitoramento.php?action=filtros' target='_blank'>API de Filtros</a><br>";
echo "<a href='api-monitoramento.php?action=check_alerts&last_check=2025-01-01T00:00:00.000Z' target='_blank'>API de Alertas</a><br>";

echo "<h2>Teste de Som</h2>";
echo "<button onclick='testSound()'>Testar Som</button>";
echo "<script>";
echo "function testSound() {";
echo "  try {";
echo "    const audioContext = new (window.AudioContext || window.webkitAudioContext)();";
echo "    const oscillator = audioContext.createOscillator();";
echo "    const gainNode = audioContext.createGain();";
echo "    oscillator.connect(gainNode);";
echo "    gainNode.connect(audioContext.destination);";
echo "    oscillator.frequency.setValueAtTime(800, audioContext.currentTime);";
echo "    gainNode.gain.setValueAtTime(0.3, audioContext.currentTime);";
echo "    oscillator.start(audioContext.currentTime);";
echo "    oscillator.stop(audioContext.currentTime + 0.5);";
echo "    alert('Som tocado com sucesso!');";
echo "  } catch (error) {";
echo "    alert('Erro ao tocar som: ' + error.message);";
echo "  }";
echo "}";
echo "</script>";
?> 
