<?php
/**
 * IMAGE PATH FIXER FOR CPANEL
 * 
 * This script identifies and fixes image path issues between localhost and cPanel.
 * It checks:
 * 1. If images are being stored with absolute paths
 * 2. If carousel.json contains the correct image references
 * 3. If upload directories are accessible on both environments
 */

error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include db_config to check environment
require_once __DIR__ . '/db_config.php';

$hostname = $_SERVER['HTTP_HOST'] ?? $_SERVER['SERVER_NAME'] ?? 'localhost';
$is_local = (strpos($hostname, 'localhost') !== false || strpos($hostname, '127.0.0.1') !== false);
$is_cpanel = !$is_local;

echo "<h1>🔍 Image Path Diagnostic Report</h1>";
echo "<p><strong>Environment:</strong> " . ($is_local ? "LOCALHOST" : "CPANEL") . "</p>";
echo "<p><strong>Hostname:</strong> " . htmlspecialchars($hostname) . "</p>";

echo "<h2>1. Checking File Paths</h2>";
echo "<table border='1' cellpadding='10'>";
echo "<tr><th>Path</th><th>Exists?</th><th>Writable?</th><th>Type</th></tr>";

$paths = [
    __DIR__ . '/uploads' => 'Main Uploads',
    __DIR__ . '/pics_ecom' => 'Legacy Pics Folder',
    __DIR__ . '/data' => 'Data Folder',
    __DIR__ . '/data/carousel.json' => 'Carousel JSON',
];

foreach ($paths as $path => $label) {
    $exists = file_exists($path) ? '✅ YES' : '❌ NO';
    $writable = is_writable($path) ? '✅ YES' : '❌ NO';
    $type = is_dir($path) ? 'Directory' : 'File';
    echo "<tr><td>$label</td><td>$exists</td><td>$writable</td><td>$type</td></tr>";
}
echo "</table>";

echo "<h2>2. Checking Carousel JSON Content</h2>";
$carouselFile = __DIR__ . '/data/carousel.json';
if (file_exists($carouselFile)) {
    $carousel = json_decode(file_get_contents($carouselFile), true);
    if ($carousel && isset($carousel['categories'])) {
        echo "<p>Found " . count($carousel['categories']) . " categories</p>";
        echo "<table border='1' cellpadding='10'>";
        echo "<tr><th>#</th><th>Name</th><th>Image Path Type</th><th>Image Preview Works?</th></tr>";
        
        foreach ($carousel['categories'] as $i => $cat) {
            $imgPath = $cat['image'] ?? '';
            
            // Detect image path type
            $pathType = 'Unknown';
            if (strpos($imgPath, 'data:image') === 0) {
                $pathType = '📊 Base64 Data URI';
            } elseif (strpos($imgPath, 'http') === 0) {
                $pathType = '🌐 Full URL';
            } elseif (strpos($imgPath, '/') === 0) {
                $pathType = '📂 Absolute Path';
            } else {
                $pathType = '📋 Relative Path';
            }
            
            $preview = 'N/A';
            if (strpos($imgPath, 'data:image') !== 0) {
                $preview = file_exists(__DIR__ . '/' . ltrim($imgPath, '/')) ? '✅ YES' : '❌ NO';
            }
            
            echo "<tr><td>" . ($i+1) . "</td><td>" . htmlspecialchars($cat['name']) . "</td><td>$pathType</td><td>$preview</td></tr>";
        }
        echo "</table>";
    }
} else {
    echo "<p>❌ carousel.json not found</p>";
}

echo "<h2>3. Image System Diagnosis</h2>";
echo "<div style='background: #f0f0f0; padding: 15px; border-radius: 5px;'>";

if ($is_cpanel) {
    echo "<h3>⚠️ CPANEL DETECTED - Possible Issues:</h3>";
    echo "<ul>";
    echo "<li><strong>Image Storage Issue:</strong> Images might be stored as base64 data URIs in carousel.json</li>";
    echo "<li><strong>Upload Directory:</strong> Check if /uploads directory exists and is writable</li>";
    echo "<li><strong>Path References:</strong> Old image paths may use /pics_ecom/ which may not be synced</li>";
    echo "<li><strong>Admin Dashboard Changes:</strong> When you update category images, they need to be saved to the correct location</li>";
    echo "</ul>";
    
    echo "<h3>✅ Solutions:</h3>";
    echo "<ol>";
    echo "<li>Ensure /uploads directory exists on cPanel and is writable (chmod 755)</li>";
    echo "<li>Update carousel image paths to use relative paths instead of base64</li>";
    echo "<li>Upload product images properly via admin panel</li>";
    echo "<li>Ensure database is synced between localhost and cPanel</li>";
    echo "</ol>";
} else {
    echo "<h3>✅ LOCALHOST - Status OK</h3>";
    echo "<p>Images should display correctly on localhost.</p>";
}

echo "</div>";

echo "<h2>4. Quick Fix Actions</h2>";
echo "<div style='background: #fffacd; padding: 15px; border-radius: 5px;'>";
echo "<p><strong>For cPanel Images Not Displaying:</strong></p>";
echo "<ol>";
echo "<li>Make sure <code>/public_html/ecoms/uploads/</code> exists</li>";
echo "<li>Set proper permissions: <code>chmod 755 /public_html/ecoms/uploads/</code></li>";
echo "<li>Re-upload category images in admin panel to store them correctly</li>";
echo "<li>Verify database has latest carousel.json data</li>";
echo "</ol>";
echo "</div>";

echo "<p><a href='admin_manage_category_carousel.php'>← Back to Carousel Manager</a></p>";
?>
