<?php
// Diagnostic script - REMOVE AFTER DEBUGGING
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);

echo "<h1>Diagnostic Check</h1>";

// 1. PHP Version
echo "<h2>1. PHP Version</h2>";
echo "<p>PHP Version: " . phpversion() . "</p>";

// 2. Check if session can start
echo "<h2>2. Session Test</h2>";
try {
    session_start();
    $_SESSION['test'] = 'working';
    echo "<p style='color:green'>✓ Session working</p>";
} catch (Exception $e) {
    echo "<p style='color:red'>✗ Session error: " . $e->getMessage() . "</p>";
}

// 3. Database connection
echo "<h2>3. Database Connection</h2>";
$db_config = [
    'hostname' => '162.215.11.244',
    'username' => 'ac3_marketing_portal',
    'password' => '=Y^3GIyrG;J)TV]z',
    'database' => 'ac3_marketing_portal',
    'port' => 3306
];

try {
    $conn = new mysqli(
        $db_config['hostname'],
        $db_config['username'],
        $db_config['password'],
        $db_config['database'],
        $db_config['port']
    );
    
    if ($conn->connect_error) {
        throw new Exception($conn->connect_error);
    }
    
    echo "<p style='color:green'>✓ Database connected</p>";
    $conn->close();
} catch (Exception $e) {
    echo "<p style='color:red'>✗ Database error: " . $e->getMessage() . "</p>";
}

// 4. Check directories
echo "<h2>4. Directory Permissions</h2>";
$dirs = [
    'application/cache',
    'application/logs',
    'uploads',
    'uploads/attachments'
];

foreach ($dirs as $dir) {
    $path = __DIR__ . '/' . $dir;
    if (is_dir($path)) {
        if (is_writable($path)) {
            echo "<p style='color:green'>✓ $dir - writable</p>";
        } else {
            echo "<p style='color:red'>✗ $dir - NOT writable</p>";
        }
    } else {
        echo "<p style='color:orange'>⚠ $dir - does not exist</p>";
    }
}

// 5. Required extensions
echo "<h2>5. PHP Extensions</h2>";
$required = ['mysqli', 'mbstring', 'curl', 'json', 'session'];
foreach ($required as $ext) {
    if (extension_loaded($ext)) {
        echo "<p style='color:green'>✓ $ext</p>";
    } else {
        echo "<p style='color:red'>✗ $ext - MISSING</p>";
    }
}

// 6. Try loading CodeIgniter
echo "<h2>6. CodeIgniter Bootstrap Test</h2>";
try {
    define('BASEPATH', __DIR__ . '/system/');
    define('APPPATH', __DIR__ . '/application/');
    define('ENVIRONMENT', 'development');
    
    // Try to load config
    if (file_exists(APPPATH . 'config/config.php')) {
        echo "<p style='color:green'>✓ config.php found</p>";
    } else {
        echo "<p style='color:red'>✗ config.php missing</p>";
    }
    
    if (file_exists(APPPATH . 'config/database.php')) {
        echo "<p style='color:green'>✓ database.php found</p>";
    } else {
        echo "<p style='color:red'>✗ database.php missing</p>";
    }
    
} catch (Exception $e) {
    echo "<p style='color:red'>✗ Bootstrap error: " . $e->getMessage() . "</p>";
}

// 7. Check .htaccess
echo "<h2>7. .htaccess Check</h2>";
if (file_exists(__DIR__ . '/.htaccess')) {
    echo "<p style='color:green'>✓ .htaccess exists</p>";
    echo "<pre>" . htmlspecialchars(file_get_contents(__DIR__ . '/.htaccess')) . "</pre>";
} else {
    echo "<p style='color:orange'>⚠ .htaccess not found</p>";
}

echo "<hr><p><strong>Diagnosis complete. Check results above.</strong></p>";
?>
