<?php
/**
 * BAITARI — DB Connection Test
 * ⚠️  DELETE THIS FILE AFTER TESTING
 * Access: https://yoursite.com/test_db.php
 */

// Only allow from localhost or specific IP for security
$allowed = ['127.0.0.1', '::1'];
// Add your IP here if testing remotely:
// $allowed[] = 'YOUR.IP.HERE';

if (!in_array($_SERVER['REMOTE_ADDR'], $allowed)) {
    // Basic auth fallback for remote access during testing
    if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] !== 'vetnet' || $_SERVER['PHP_AUTH_PW'] !== 'test2024') {
        header('WWW-Authenticate: Basic realm="Test"');
        header('HTTP/1.0 401 Unauthorized');
        die('Unauthorized');
    }
}

require_once __DIR__ . '/includes/config.php';

echo '<style>body{font-family:monospace;padding:20px;background:#0a1a12;color:#4aba8a;}</style>';
echo '<h2>🐾 Baitari — DB Test</h2>';
echo '<pre>';

// Test 1: Connection
try {
    $pdo = db();
    echo "✅ DB Connection: OK\n";
    echo "   Host:    " . DB_HOST . "\n";
    echo "   DB Name: " . DB_NAME . "\n";
    echo "   User:    " . DB_USER . "\n\n";
} catch (Exception $e) {
    echo "❌ DB Connection FAILED: " . $e->getMessage() . "\n";
    exit;
}

// Test 2: Tables
$tables = $pdo->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
echo "📋 Tables found: " . count($tables) . "\n";
foreach ($tables as $t) {
    $count = $pdo->query("SELECT COUNT(*) FROM `$t`")->fetchColumn();
    echo "   - $t ($count rows)\n";
}
echo "\n";

// Test 3: Sample users
$users = $pdo->query("SELECT id, name, role, phone FROM baitari_users LIMIT 5")->fetchAll();
echo "👥 Sample users:\n";
foreach ($users as $u) {
    echo "   [{$u['role']}] {$u['name']} — {$u['phone']}\n";
}
echo "\n";

// Test 4: PHP version
echo "🐘 PHP Version: " . PHP_VERSION . "\n";
echo "⏰ Server Time: " . date('Y-m-d H:i:s') . "\n";
echo "\n⚠️  DELETE test_db.php after testing!\n";
echo '</pre>';
