<?php
// Server configuration check
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Server Configuration Check</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .status { padding: 10px; margin: 10px 0; border-radius: 5px; }
        .success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
        .error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
        .warning { background-color: #fff3cd; color: #856404; border: 1px solid #ffeaa7; }
        .info { background-color: #d1ecf1; color: #0c5460; border: 1px solid #bee5eb; }
        pre { background: #f8f9fa; padding: 15px; border-radius: 5px; overflow-x: auto; }
    </style>
</head>
<body>
    <h1>🔧 Server Configuration Check</h1>
    
    <div class="info">
        <h3>PHP Status</h3>
        <p><strong>PHP Version:</strong> <?php echo phpversion(); ?></p>
        <p><strong>Server Software:</strong> <?php echo $_SERVER['SERVER_SOFTWARE'] ?? 'Unknown'; ?></p>
        <p><strong>Document Root:</strong> <?php echo $_SERVER['DOCUMENT_ROOT'] ?? 'Unknown'; ?></p>
        <p><strong>Script Name:</strong> <?php echo $_SERVER['SCRIPT_NAME'] ?? 'Unknown'; ?></p>
        <p><strong>Request URI:</strong> <?php echo $_SERVER['REQUEST_URI'] ?? 'Unknown'; ?></p>
    </div>

    <div class="info">
        <h3>PHP Extensions</h3>
        <p><strong>cURL:</strong> <?php echo extension_loaded('curl') ? '✅ Available' : '❌ Missing'; ?></p>
        <p><strong>JSON:</strong> <?php echo extension_loaded('json') ? '✅ Available' : '❌ Missing'; ?></p>
        <p><strong>PDO:</strong> <?php echo extension_loaded('pdo') ? '✅ Available' : '❌ Missing'; ?></p>
        <p><strong>MySQL:</strong> <?php echo extension_loaded('pdo_mysql') ? '✅ Available' : '❌ Missing'; ?></p>
    </div>

    <div class="info">
        <h3>File Permissions</h3>
        <p><strong>Current File:</strong> <?php echo __FILE__; ?></p>
        <p><strong>File Permissions:</strong> <?php echo substr(sprintf('%o', fileperms(__FILE__)), -4); ?></p>
        <p><strong>Directory Permissions:</strong> <?php echo substr(sprintf('%o', fileperms(dirname(__FILE__))), -4); ?></p>
        <p><strong>Writable:</strong> <?php echo is_writable(__FILE__) ? '✅ Yes' : '❌ No'; ?></p>
    </div>

    <div class="info">
        <h3>Server Headers</h3>
        <pre><?php print_r(getallheaders()); ?></pre>
    </div>

    <div class="info">
        <h3>Environment Variables</h3>
        <pre><?php 
        $env_vars = [
            'REQUEST_METHOD' => $_SERVER['REQUEST_METHOD'] ?? 'Not Set',
            'HTTP_HOST' => $_SERVER['HTTP_HOST'] ?? 'Not Set',
            'HTTPS' => $_SERVER['HTTPS'] ?? 'Not Set',
            'SERVER_PORT' => $_SERVER['SERVER_PORT'] ?? 'Not Set',
            'QUERY_STRING' => $_SERVER['QUERY_STRING'] ?? 'Not Set',
        ];
        print_r($env_vars);
        ?></pre>
    </div>

    <div class="info">
        <h3>PHP Configuration</h3>
        <pre><?php 
        $php_config = [
            'display_errors' => ini_get('display_errors'),
            'error_reporting' => ini_get('error_reporting'),
            'log_errors' => ini_get('log_errors'),
            'error_log' => ini_get('error_log'),
            'max_execution_time' => ini_get('max_execution_time'),
            'memory_limit' => ini_get('memory_limit'),
            'upload_max_filesize' => ini_get('upload_max_filesize'),
            'post_max_size' => ini_get('post_max_size'),
        ];
        print_r($php_config);
        ?></pre>
    </div>

    <div class="info">
        <h3>Test Actions</h3>
        <p><a href="test_php.php" target="_blank">🧪 Test PHP JSON Response</a></p>
        <p><a href="phpinfo.php" target="_blank">📊 View PHP Info</a></p>
        <p><a href="phonepe_v2_backend.php" target="_blank">🔗 Test PhonePe Backend</a></p>
    </div>

    <div class="warning">
        <h3>⚠️ Security Note</h3>
        <p>Remove <code>phpinfo.php</code> and <code>server_check.php</code> after testing for security reasons.</p>
    </div>

    <script>
        // Test AJAX request
        function testAjax() {
            fetch('test_php.php')
                .then(response => response.json())
                .then(data => {
                    console.log('AJAX Test Result:', data);
                    alert('AJAX Test Successful: ' + JSON.stringify(data));
                })
                .catch(error => {
                    console.error('AJAX Test Error:', error);
                    alert('AJAX Test Failed: ' + error.message);
                });
        }
    </script>

    <button onclick="testAjax()">🧪 Test AJAX Request</button>
</body>
</html>
