<?php
require_once __DIR__ . '/vendor/autoload.php';

date_default_timezone_set('America/Sao_Paulo');

use App\Config\Database;

$conn = Database::getInstance()->getConnection();

echo "PHP Time: " . date('Y-m-d H:i:s') . " (" . date_default_timezone_get() . ")\n";
echo "PHP Timestamp: " . time() . "\n";

$stmt = $conn->query("SELECT NOW() as db_time, @@global.time_zone as global_tz, @@session.time_zone as session_tz");
$row = $stmt->fetch(PDO::FETCH_ASSOC);

echo "DB Time: " . $row['db_time'] . "\n";
echo "DB Global TZ: " . $row['global_tz'] . "\n";
echo "DB Session TZ: " . $row['session_tz'] . "\n";

echo "\n--- Recent API Logs ---\n";
$stmtLogs = $conn->query("SELECT * FROM api_logs ORDER BY id DESC LIMIT 5");
$logs = $stmtLogs->fetchAll(PDO::FETCH_ASSOC);
print_r($logs);

echo "\n--- Server Status ---\n";
$stmtServers = $conn->query("SELECT id, name, last_seen FROM servers");
$servers = $stmtServers->fetchAll(PDO::FETCH_ASSOC);
foreach ($servers as $server) {
    echo "Server: {$server['name']}, Last Seen: {$server['last_seen']}\n";
    $lastSeen = !empty($server['last_seen']) ? strtotime($server['last_seen']) : 0;
    $diff = time() - $lastSeen;
    echo "Diff: $diff seconds\n";
    echo "Is Online (< 300): " . ($diff < 300 ? 'Yes' : 'No') . "\n";
}
