<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>🔧 Teste de Conexão - Banco de Testes</h1>";

require_once 'conexao_teste.php';

echo "<h2>1. Verificando conexão:</h2>";
if (isset($conn_teste) && $conn_teste instanceof mysqli) {
    echo "<p style='color:green'>✅ Conexão estabelecida</p>";
    echo "<p>Host: " . $conn_teste->host_info . "</p>";
} else {
    echo "<p style='color:red'>❌ Falha na conexão</p>";
}

echo "<h2>2. Verificando tabela 'usuarios':</h2>";
$result = $conn_teste->query("SHOW TABLES LIKE 'usuarios'");
if ($result->num_rows > 0) {
    echo "<p style='color:green'>✅ Tabela 'usuarios' existe</p>";
} else {
    echo "<p style='color:red'>❌ Tabela 'usuarios' NÃO existe</p>";
}

echo "<h2>3. Total de usuários:</h2>";
$result = $conn_teste->query("SELECT COUNT(*) as total FROM usuarios");
$row = $result->fetch_assoc();
echo "<p>Total de usuários: <strong>" . $row['total'] . "</strong></p>";

echo "<h2>4. Listando usuários (máx 5):</h2>";
$result = $conn_teste->query("SELECT id, nome, email, ativo, tenant_id FROM usuarios LIMIT 5");
if ($result->num_rows > 0) {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>ID</th><th>Nome</th><th>Email</th><th>Ativo</th><th>tenant_id</th></tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row['id'] . "</td>";
        echo "<td>" . $row['nome'] . "</td>";
        echo "<td>" . $row['email'] . "</td>";
        echo "<td>" . $row['ativo'] . "</td>";
        echo "<td>" . $row['tenant_id'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "<p>Nenhum usuário encontrado</p>";
}

echo "<h2>5. Verificando estrutura da tabela:</h2>";
$result = $conn_teste->query("DESCRIBE usuarios");
if ($result) {
    echo "<table border='1' cellpadding='5'>";
    echo "<tr><th>Campo</th><th>Tipo</th><th>Null</th><th>Key</th></tr>";
    while ($row = $result->fetch_assoc()) {
        echo "<tr>";
        echo "<td>" . $row['Field'] . "</td>";
        echo "<td>" . $row['Type'] . "</td>";
        echo "<td>" . $row['Null'] . "</td>";
        echo "<td>" . $row['Key'] . "</td>";
        echo "</tr>";
    }
    echo "</table>";
}

$conn_teste->close();
?>