<?php
/**
 * Simple test page to verify the application is running
 * Access via: http://10.10.1.145:8085/All_Marketing_Portals/auriga_marketing_portal/test_connection.php
 */

// Test database connection
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'marketing_portal';

echo "<!DOCTYPE html><html><head><title>Connection Test</title>";
echo "<style>body{font-family:Arial;padding:20px;} .success{color:green;} .error{color:red;} .info{color:blue;}</style>";
echo "</head><body>";
echo "<h1>Auriga Portal - Connection Test</h1>";

// Test 1: PHP Version
echo "<h2>1. PHP Version</h2>";
echo "<p class='info'>PHP Version: " . phpversion() . "</p>";

// Test 2: Required Extensions
echo "<h2>2. Required Extensions</h2>";
$extensions = ['mysqli', 'mbstring', 'json', 'session'];
foreach ($extensions as $ext) {
    $loaded = extension_loaded($ext);
    $class = $loaded ? 'success' : 'error';
    $status = $loaded ? '✓ Loaded' : '✗ Missing';
    echo "<p class='$class'>$ext: $status</p>";
}

// Test 3: Database Connection
echo "<h2>3. Database Connection</h2>";
try {
    $conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
    
    if ($conn->connect_error) {
        echo "<p class='error'>✗ Connection failed: " . $conn->connect_error . "</p>";
    } else {
        echo "<p class='success'>✓ Database connected successfully</p>";
        
        // Test query
        $result = $conn->query("SELECT COUNT(*) as total FROM campaigns");
        if ($result) {
            $row = $result->fetch_assoc();
            echo "<p class='info'>Total Campaigns: " . $row['total'] . "</p>";
        }
        
        $result = $conn->query("SELECT COUNT(*) as total FROM users");
        if ($result) {
            $row = $result->fetch_assoc();
            echo "<p class='info'>Total Users: " . $row['total'] . "</p>";
        }
        
        $conn->close();
    }
} catch (Exception $e) {
    echo "<p class='error'>✗ Database error: " . $e->getMessage() . "</p>";
}

// Test 4: File Permissions
echo "<h2>4. File Permissions</h2>";
$writable_dirs = [
    'application/cache',
    'application/logs',
    'uploads'
];

foreach ($writable_dirs as $dir) {
    $path = __DIR__ . '/' . $dir;
    if (is_dir($path)) {
        $writable = is_writable($path);
        $class = $writable ? 'success' : 'error';
        $status = $writable ? '✓ Writable' : '✗ Not writable';
        echo "<p class='$class'>$dir: $status</p>";
    } else {
        echo "<p class='error'>$dir: ✗ Directory not found</p>";
    }
}

// Test 5: URL Configuration
echo "<h2>5. URL Configuration</h2>";
echo "<p class='info'>Current URL: " . $_SERVER['REQUEST_URI'] . "</p>";
echo "<p class='info'>HTTP Host: " . $_SERVER['HTTP_HOST'] . "</p>";
echo "<p class='info'>Server Software: " . $_SERVER['SERVER_SOFTWARE'] . "</p>";

echo "<hr>";
echo "<p><strong>If all tests pass, the application is ready to use!</strong></p>";
echo "<p><a href='index.php'>Go to Application</a></p>";
echo "</body></html>";
?>
