<?php
// ============================================
// System Health Check
// ============================================
// This file helps verify your deployment is configured correctly
// Remove this file after testing

require_once 'config.php';

$results = [];
$errors = [];
$warnings = [];

// Check PHP version
if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
    $results[] = "✓ PHP Version: " . PHP_VERSION . " (OK)";
} else {
    $errors[] = "✗ PHP Version: " . PHP_VERSION . " (Minimum 7.4 required)";
}

// Check required PHP extensions
$extensions = ['pdo', 'pdo_mysql', 'json', 'gd', 'mbstring'];
foreach ($extensions as $ext) {
    if (extension_loaded($ext)) {
        $results[] = "✓ PHP Extension: {$ext}";
    } else {
        $errors[] = "✗ PHP Extension: {$ext} (NOT FOUND)";
    }
}

// Check database connection
try {
    $db = getDB();
    $results[] = "✓ Database connection successful";
    
    // Check database tables
    $stmt = $db->query("SHOW TABLES");
    $tables = $stmt->fetchAll(PDO::FETCH_COLUMN);
    $results[] = "✓ Database tables found: " . count($tables);
} catch (Exception $e) {
    $errors[] = "✗ Database connection failed: " . $e->getMessage();
}

// Check file/directory permissions
$directories = [
    'temp' => 0755,
    'vendor' => 0755,
    'assets' => 0755,
    'includes' => 0755,
    'pages' => 0755
];

foreach ($directories as $dir => $perms) {
    if (is_dir($dir)) {
        if (is_writable($dir)) {
            $results[] = "✓ Directory writable: {$dir}";
        } else {
            $warnings[] = "⚠ Directory not writable: {$dir} (may cause issues)";
        }
    } else {
        $errors[] = "✗ Directory not found: {$dir}";
    }
}

// Check config file
if (file_exists('.env')) {
    $results[] = "✓ .env configuration file found";
} else {
    $warnings[] = "⚠ .env file not found (using defaults)";
}

// Check vendor autoloader
if (file_exists('vendor/autoload.php')) {
    $results[] = "✓ Composer autoloader found";
} else {
    $errors[] = "✗ Composer autoloader not found (run: composer install)";
}

// Check key functions
if (function_exists('getDB')) {
    $results[] = "✓ Database functions available";
} else {
    $errors[] = "✗ Database functions not available";
}

// Environment info
$results[] = "\nEnvironment Information:";
$results[] = "- APP_ENV: " . APP_ENV;
$results[] = "- APP_NAME: " . APP_NAME;
$results[] = "- APP_VERSION: " . APP_VERSION;
$results[] = "- APP_DEBUG: " . (APP_DEBUG ? 'true' : 'false');

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JBR Sofa - System Health Check</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); min-height: 100vh; padding: 20px; }
        .container { max-width: 900px; margin: 0 auto; background: white; border-radius: 12px; box-shadow: 0 20px 60px rgba(0,0,0,0.3); overflow: hidden; }
        .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 40px; text-align: center; }
        .header h1 { font-size: 32px; margin-bottom: 10px; }
        .header p { font-size: 14px; opacity: 0.9; }
        .content { padding: 40px; }
        .section { margin-bottom: 30px; }
        .section h2 { font-size: 18px; color: #333; margin-bottom: 15px; border-bottom: 2px solid #eee; padding-bottom: 10px; }
        .result { margin: 10px 0; padding: 10px 15px; border-radius: 6px; font-family: 'Monaco', 'Courier New', monospace; font-size: 13px; }
        .result.success { background: #ecfdf5; color: #065f46; border-left: 4px solid #10b981; }
        .result.error { background: #fef2f2; color: #991b1b; border-left: 4px solid #ef4444; }
        .result.warning { background: #fffbeb; color: #92400e; border-left: 4px solid #f59e0b; }
        .summary { margin-top: 40px; padding: 20px; background: #f9fafb; border-radius: 8px; }
        .summary-item { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid #e5e7eb; }
        .summary-item:last-child { border-bottom: none; }
        .summary-value { font-weight: 600; }
        .status-badge { display: inline-block; padding: 6px 12px; border-radius: 20px; font-size: 12px; font-weight: 600; margin-top: 20px; text-align: center; }
        .status-badge.ok { background: #10b981; color: white; }
        .status-badge.warning { background: #f59e0b; color: white; }
        .status-badge.error { background: #ef4444; color: white; }
        .footer { padding: 20px 40px; background: #f9fafb; border-top: 1px solid #e5e7eb; text-align: center; color: #666; font-size: 12px; }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🔍 JBR Sofa - System Health Check</h1>
            <p>Verify your deployment configuration</p>
        </div>
        
        <div class="content">
            <?php if (!empty($results)): ?>
            <div class="section">
                <h2>✓ System Status</h2>
                <?php foreach ($results as $result): ?>
                    <div class="result success"><?= htmlspecialchars($result) ?></div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
            <?php if (!empty($warnings)): ?>
            <div class="section">
                <h2>⚠ Warnings</h2>
                <?php foreach ($warnings as $warning): ?>
                    <div class="result warning"><?= htmlspecialchars($warning) ?></div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
            <?php if (!empty($errors)): ?>
            <div class="section">
                <h2>✗ Errors</h2>
                <?php foreach ($errors as $error): ?>
                    <div class="result error"><?= htmlspecialchars($error) ?></div>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            
            <div class="summary">
                <div class="summary-item">
                    <span>PHP Version</span>
                    <span class="summary-value"><?= PHP_VERSION ?></span>
                </div>
                <div class="summary-item">
                    <span>Environment</span>
                    <span class="summary-value"><?= APP_ENV ?></span>
                </div>
                <div class="summary-item">
                    <span>Debug Mode</span>
                    <span class="summary-value"><?= APP_DEBUG ? 'Enabled' : 'Disabled' ?></span>
                </div>
                <div class="summary-item">
                    <span>Database</span>
                    <span class="summary-value"><?= DB_NAME ?> @ <?= DB_HOST ?></span>
                </div>
                <div class="summary-item">
                    <span>Total Checks</span>
                    <span class="summary-value"><?= count($results) + count($warnings) + count($errors) ?></span>
                </div>
            </div>
            
            <?php 
            if (empty($errors) && empty($warnings)) {
                echo '<div class="status-badge ok">✓ All Systems Operational</div>';
            } elseif (empty($errors)) {
                echo '<div class="status-badge warning">⚠ Some Warnings - Please Review</div>';
            } else {
                echo '<div class="status-badge error">✗ Issues Detected - Please Fix Before Deployment</div>';
            }
            ?>
        </div>
        
        <div class="footer">
            <p>ℹ️ Delete this file (system-check.php) after testing. For troubleshooting, see DEPLOYMENT.md</p>
        </div>
    </div>
</body>
</html>
