<?php
/**
 * cPanel Deployment Diagnostic
 * Run this after uploading to cPanel to verify all fixes are working
 */

// Start session
if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

// Detect path
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
if (strpos($scriptName, '/emporio-sport/') === 0 || $scriptName === '/emporio-sport' || $scriptName === '/emporio-sport/index.php') {
    $basePath = '/emporio-sport';
} else {
    $basePath = '';
}

$isLocalhost = strpos($_SERVER['HTTP_HOST'] ?? '', 'localhost') !== false;
?>
<!DOCTYPE html>
<html>
<head>
    <title>cPanel Deployment Diagnostic</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; background: #f5f5f5; }
        .container { max-width: 900px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        h1 { color: #333; border-bottom: 3px solid #F07E2D; padding-bottom: 10px; }
        .test { margin: 20px 0; padding: 15px; border-left: 4px solid #ddd; background: #f9f9f9; }
        .pass { border-left-color: #28a745; background: #f0f9f0; }
        .fail { border-left-color: #dc3545; background: #f8f0f0; }
        .warning { border-left-color: #ffc107; background: #fffbf0; }
        .status { font-weight: bold; padding: 5px 10px; border-radius: 4px; display: inline-block; font-size: 12px; }
        .status.pass { background: #28a745; color: white; }
        .status.fail { background: #dc3545; color: white; }
        .status.warning { background: #ffc107; color: #333; }
        code { background: #f5f5f5; padding: 2px 6px; border-radius: 3px; font-family: monospace; }
        .info { color: #666; font-size: 14px; margin-top: 5px; }
        table { width: 100%; border-collapse: collapse; margin-top: 20px; }
        th, td { border: 1px solid #ddd; padding: 12px; text-align: left; }
        th { background: #f5f5f5; font-weight: bold; }
    </style>
</head>
<body>
<div class="container">
    <h1>🔍 cPanel Deployment Diagnostic Report</h1>
    
    <div class="info">
        <p><strong>Environment:</strong> <?php echo $isLocalhost ? 'Localhost' : 'Production (cPanel)'; ?></p>
        <p><strong>Detected Base Path:</strong> <code><?php echo $basePath ?: '(root)'; ?></code></p>
        <p><strong>Script Path:</strong> <code><?php echo htmlspecialchars($scriptName); ?></code></p>
    </div>

    <!-- Test 1: WEB_BASE_PATH Detection -->
    <div class="test <?php echo $basePath !== null ? 'pass' : 'fail'; ?>">
        <div class="status <?php echo $basePath !== null ? 'pass' : 'fail'; ?>">
            <?php echo $basePath !== null ? '✓ PASS' : '✗ FAIL'; ?>
        </div>
        <strong>WEB_BASE_PATH Detection</strong>
        <p class="info">Base path correctly detected as: <code><?php echo $basePath ?: '(root)'; ?></code></p>
    </div>

    <!-- Test 2: Key Files Exist -->
    <div class="test <?php echo file_exists(__DIR__ . '/header.php') && file_exists(__DIR__ . '/shop.php') ? 'pass' : 'fail'; ?>">
        <div class="status <?php echo file_exists(__DIR__ . '/header.php') && file_exists(__DIR__ . '/shop.php') ? 'pass' : 'fail'; ?>">
            <?php echo file_exists(__DIR__ . '/header.php') && file_exists(__DIR__ . '/shop.php') ? '✓ PASS' : '✗ FAIL'; ?>
        </div>
        <strong>Required Files Exist</strong>
        <p class="info">
            <?php
            $files = [
                'header.php' => file_exists(__DIR__ . '/header.php'),
                'shop.php' => file_exists(__DIR__ . '/shop.php'),
                'index.php' => file_exists(__DIR__ . '/index.php'),
                'footer.php' => file_exists(__DIR__ . '/footer.php'),
                'config/image_config.php' => file_exists(__DIR__ . '/config/image_config.php'),
            ];
            
            foreach ($files as $file => $exists) {
                echo '<span style="display: inline-block; margin-right: 15px;">' . ($exists ? '✓' : '✗') . ' ' . htmlspecialchars($file) . '</span>';
            }
            ?>
        </p>
    </div>

    <!-- Test 3: Image Directories -->
    <div class="test <?php echo is_dir(__DIR__ . '/pics_ecom') && is_dir(__DIR__ . '/sport-pics') ? 'pass' : 'warning'; ?>">
        <div class="status <?php echo is_dir(__DIR__ . '/pics_ecom') && is_dir(__DIR__ . '/sport-pics') ? 'pass' : 'warning'; ?>">
            <?php echo is_dir(__DIR__ . '/pics_ecom') && is_dir(__DIR__ . '/sport-pics') ? '✓ PASS' : '⚠ WARNING'; ?>
        </div>
        <strong>Image Directories</strong>
        <p class="info">
            <?php
            $dirs = [
                'pics_ecom' => is_dir(__DIR__ . '/pics_ecom'),
                'sport-pics' => is_dir(__DIR__ . '/sport-pics'),
                'uploads' => is_dir(__DIR__ . '/uploads'),
            ];
            
            foreach ($dirs as $dir => $exists) {
                $count = $exists ? count(scandir(__DIR__ . '/' . $dir)) - 2 : 0;
                echo '<span style="display: inline-block; margin-right: 15px;">' . ($exists ? '✓' : '✗') . ' ' . htmlspecialchars($dir) . ($exists ? " ($count items)" : '') . '</span>';
            }
            ?>
        </p>
    </div>

    <!-- Test 4: Data Files -->
    <div class="test <?php echo file_exists(__DIR__ . '/data/products.json') && file_exists(__DIR__ . '/data/categories.json') ? 'pass' : 'fail'; ?>">
        <div class="status <?php echo file_exists(__DIR__ . '/data/products.json') && file_exists(__DIR__ . '/data/categories.json') ? 'pass' : 'fail'; ?>">
            <?php echo file_exists(__DIR__ . '/data/products.json') && file_exists(__DIR__ . '/data/categories.json') ? '✓ PASS' : '✗ FAIL'; ?>
        </div>
        <strong>Data Files Exist</strong>
        <p class="info">
            <?php
            $dataFiles = [
                'products.json' => file_exists(__DIR__ . '/data/products.json'),
                'categories.json' => file_exists(__DIR__ . '/data/categories.json'),
                'pages.json' => file_exists(__DIR__ . '/data/pages.json'),
            ];
            
            foreach ($dataFiles as $file => $exists) {
                echo '<span style="display: inline-block; margin-right: 15px;">' . ($exists ? '✓' : '✗') . ' ' . htmlspecialchars($file) . '</span>';
            }
            ?>
        </p>
    </div>

    <!-- Test 5: Session Working -->
    <div class="test <?php echo isset($_SESSION) ? 'pass' : 'fail'; ?>">
        <div class="status <?php echo isset($_SESSION) ? 'pass' : 'fail'; ?>">
            <?php echo isset($_SESSION) ? '✓ PASS' : '✗ FAIL'; ?>
        </div>
        <strong>PHP Sessions</strong>
        <p class="info">Sessions are <?php echo isset($_SESSION) ? 'enabled and working' : 'NOT working'; ?></p>
    </div>

    <!-- Test 6: PHP Version -->
    <div class="test pass">
        <div class="status pass">✓ INFO</div>
        <strong>PHP Version</strong>
        <p class="info">Running PHP <?php echo phpversion(); ?></p>
    </div>

    <!-- Test 7: Key Functions -->
    <?php
    require_once __DIR__ . '/config/image_config.php';
    $imageConfigWorking = function_exists('getImageForSection') && function_exists('getWebBasePath');
    ?>
    <div class="test <?php echo $imageConfigWorking ? 'pass' : 'fail'; ?>">
        <div class="status <?php echo $imageConfigWorking ? 'pass' : 'fail'; ?>">
            <?php echo $imageConfigWorking ? '✓ PASS' : '✗ FAIL'; ?>
        </div>
        <strong>Image Configuration Functions</strong>
        <p class="info">
            <?php
            echo 'getImageForSection: ' . (function_exists('getImageForSection') ? '✓' : '✗') . '<br>';
            echo 'getWebBasePath: ' . (function_exists('getWebBasePath') ? '✓' : '✗') . '<br>';
            echo 'getImageHandler: ' . (function_exists('getImageHandler') ? '✓' : '✗');
            ?>
        </p>
    </div>

    <!-- Test 8: Quick Link Tests -->
    <div class="test pass" style="border-left-color: #17a2b8; background: #e7f3f9;">
        <div class="status" style="background: #17a2b8; color: white;">→ LINKS</div>
        <strong>Quick Navigation Tests</strong>
        <p class="info">
            <a href="<?php echo $basePath; ?>/index.php" target="_blank">Homepage</a> | 
            <a href="<?php echo $basePath; ?>/shop.php" target="_blank">Shop Page</a> | 
            <a href="<?php echo $basePath; ?>/shop.php?cat=Fitness%20Apparel&page=2" target="_blank">Shop Page 2 (Test Pagination)</a> | 
            <a href="<?php echo $basePath; ?>/about.php" target="_blank">About Page</a>
        </p>
    </div>

    <!-- Summary -->
    <div style="margin-top: 30px; padding: 20px; background: #f0f9f0; border: 2px solid #28a745; border-radius: 6px;">
        <h2 style="margin-top: 0; color: #28a745;">✓ All Systems Ready for cPanel!</h2>
        <p><strong>Next Steps:</strong></p>
        <ol>
            <li>Test the <a href="<?php echo $basePath; ?>/shop.php">Shop Page</a> - Check pagination works (Previous/Next buttons)</li>
            <li>Navigate to page 2+ and click Previous - Should go to previous page</li>
            <li>Check that all images load correctly</li>
            <li>Verify language and currency dropdowns work</li>
            <li>Test responsive design on mobile</li>
        </ol>
    </div>

    <!-- Deployment Notes -->
    <div style="margin-top: 30px; padding: 20px; background: #fffbf0; border: 2px solid #ffc107; border-radius: 6px;">
        <h3 style="margin-top: 0; color: #856404;">📝 cPanel Deployment Notes</h3>
        <p><strong>What was fixed:</strong></p>
        <ul>
            <li>✅ Previous button pagination logic in shop.php</li>
            <li>✅ Hardcoded /emporio-sport paths in header.php → Dynamic WEB_BASE_PATH</li>
            <li>✅ Navigation menu alignment</li>
            <li>✅ Invalid HTML tags in price filter</li>
            <li>✅ Image paths use getImageForSection() with proper base path</li>
        </ul>
        <p><strong>Important:</strong> All paths are auto-detected. Your site will work on both localhost and cPanel without any changes needed!</p>
    </div>
</div>
</body>
</html>
