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

echo "<!DOCTYPE html>
<html>
<head>
    <title>Teste DNS Centralizado</title>
    <style>
        body { font-family: Arial; padding: 20px; }
        .success { color: green; }
        .error { color: red; }
        .info { color: blue; }
        table { border-collapse: collapse; width: 100%; }
        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
        th { background-color: #f2f2f2; }
    </style>
</head>
<body>
    <h1>Teste do Sistema Centralizado de DNS</h1>";

require_once 'includes/DNSDatabase.php';

$dnsDB = DNSDatabase::getInstance();

echo "<h2>1. Teste de Conexão</h2>";
echo "<p class='success'>✓ Banco de dados conectado com sucesso</p>";

echo "<h2>2. Criar DNS de Teste</h2>";

// Criar DNS para Admin
$admin_result = $dnsDB->create('DNS Admin Teste', 'http://admin-teste.com:8080', 'admin', 'admin');
echo "<p>" . ($admin_result ? "<span class='success'>✓</span> DNS Admin criada" : "<span class='error'>✗</span> Erro ao criar DNS Admin") . "</p>";

// Criar DNS para Revenda (aa)
$revenda_result = $dnsDB->create('DNS Revenda Teste', 'http://revenda-teste.com:8080', 'revenda', 'aa');
echo "<p>" . ($revenda_result ? "<span class='success'>✓</span> DNS Revenda criada" : "<span class='error'>✗</span> Erro ao criar DNS Revenda") . "</p>";

// Criar DNS para Sub Revenda
$sub_result = $dnsDB->create('DNS Sub Teste', 'http://sub-teste.com:8080', 'sub_revenda', 'teste');
echo "<p>" . ($sub_result ? "<span class='success'>✓</span> DNS Sub Revenda criada" : "<span class='error'>✗</span> Erro ao criar DNS Sub Revenda") . "</p>";

echo "<h2>3. Listar Todas DNSs</h2>";
$all_dns = $dnsDB->getAllDNS();
echo "<table>";
echo "<tr><th>ID</th><th>Nome</th><th>DNS</th><th>Tipo</th><th>Proprietário</th><th>Criado em</th></tr>";
while ($row = $all_dns->fetchArray(SQLITE3_ASSOC)) {
    echo "<tr>";
    echo "<td>{$row['id']}</td>";
    echo "<td>" . htmlspecialchars($row['nome']) . "</td>";
    echo "<td>" . htmlspecialchars($row['dns']) . "</td>";
    echo "<td>{$row['owner_type']}</td>";
    echo "<td>{$row['owner_name']}</td>";
    echo "<td>{$row['created_at']}</td>";
    echo "</tr>";
}
echo "</table>";

echo "<h2>4. Contar DNSs por Tipo</h2>";
$admin_count = $dnsDB->getCount('admin', 'admin');
$revenda_count = $dnsDB->getCount('revenda', 'aa');
echo "<p>Admin: <strong>$admin_count</strong> DNS(s)</p>";
echo "<p>Revenda (aa): <strong>$revenda_count</strong> DNS(s)</p>";

echo "<h2>5. Links Úteis</h2>";
echo "<ul>";
echo "<li><a href='dns_manager.php'>Gerenciar DNSs - Admin</a></li>";
echo "<li><a href='revenda/dns_manager.php'>Gerenciar DNSs - Revenda</a></li>";
echo "<li><a href='sub_revenda/dns_manager.php'>Gerenciar DNSs - Sub Revenda</a></li>";
echo "</ul>";

$dnsDB->close();

echo "</body></html>";
?>