<?php
// Simple Database Connection Test
// Upload and visit: https://brandwave.aurigaconsulting.co.in/test_db_simple.php

error_reporting(E_ALL);
ini_set('display_errors', 1);

echo "<h1>Database Connection Test</h1>";
echo "<style>body{font-family:Arial;padding:20px;}.ok{color:green;}.err{color:red;}</style>";

$host = '162.215.11.244';
$user = 'ac3_marketing_portal';
$pass = '=Y^3GIyrG;J)TV]z';
$db   = 'ac3_marketing_portal';
$port = 3306;

echo "<h2>Testing Connection...</h2>";
echo "<p>Host: $host:$port</p>";
echo "<p>Database: $db</p>";

$mysqli = new mysqli($host, $user, $pass, $db, $port);

if ($mysqli->connect_error) {
    echo "<p class='err'>❌ Connection FAILED: " . htmlspecialchars($mysqli->connect_error) . "</p>";
    echo "<h3>Possible Solutions:</h3>";
    echo "<ul>";
    echo "<li>Check if database server allows connections from this IP</li>";
    echo "<li>Verify firewall allows port 3306</li>";
    echo "<li>Check database credentials</li>";
    echo "<li>Contact hosting provider</li>";
    echo "</ul>";
    exit;
}

echo "<p class='ok'>✅ Connection SUCCESSFUL!</p>";

// Test portal
$result = $mysqli->query("SELECT * FROM portals WHERE id = 3");
if ($result && $result->num_rows > 0) {
    $portal = $result->fetch_assoc();
    echo "<p class='ok'>✅ Portal #3 exists: " . htmlspecialchars($portal['name']) . "</p>";
} else {
    echo "<p class='err'>❌ Portal #3 NOT FOUND in database!</p>";
    echo "<p>Run the SQL commands in fix_database.sql</p>";
}

// Test users
$result = $mysqli->query("SELECT COUNT(*) as cnt FROM users");
if ($result) {
    $row = $result->fetch_assoc();
    echo "<p class='ok'>✅ Total users: " . $row['cnt'] . "</p>";
}

// Test tables
$tables = ['users', 'portals', 'campaigns', 'login_logs', 'audit_logs'];
echo "<h3>Tables Check:</h3>";
foreach ($tables as $table) {
    $result = $mysqli->query("SHOW TABLES LIKE '$table'");
    if ($result && $result->num_rows > 0) {
        echo "<p class='ok'>✅ $table</p>";
    } else {
        echo "<p class='err'>❌ $table missing</p>";
    }
}

$mysqli->close();
echo "<hr><p><strong>If all checks pass, the application should work!</strong></p>";
?>
