<?php
session_start();
require_once 'config/database.php';
require_once 'includes/site_settings.php';

echo "<h2>Certificate Logo Source Investigation</h2>";

// Get site settings
$settings = get_all_settings($pdo);
$logo_image = $settings['logo_image'] ?? null;

echo "<h3>1. Site Settings Logo</h3>";
echo "Logo from site settings: " . ($logo_image ?? 'NONE') . "<br>";

if ($logo_image) {
    $full_path = __DIR__ . '/' . ltrim($logo_image, './');
    echo "Full path: $full_path<br>";
    echo "File exists: " . (file_exists($full_path) ? 'YES' : 'NO') . "<br>";
    if (file_exists($full_path)) {
        echo "<img src='$logo_image' style='max-height: 100px; border: 1px solid #ccc;' alt='Site Logo'><br>";
    }
}

echo "<h3>2. Default/Fallback Logos</h3>";

// Check for default logo files
$default_logos = [
    'assets/images/logo.png',
    'assets/images/logo.jpg',
    'uploads/settings/school_logo*'
];

foreach ($default_logos as $logo_path) {
    if (strpos($logo_path, '*') !== false) {
        // Handle wildcard
        $files = glob($logo_path);
        foreach ($files as $file) {
            echo "Found: $file - " . (file_exists($file) ? 'EXISTS' : 'MISSING') . "<br>";
            if (file_exists($file)) {
                $relative_path = str_replace(__DIR__ . '\\', '', $file);
                echo "<img src='$relative_path' style='max-height: 80px; border: 1px solid #ccc;' alt='Default Logo'><br>";
            }
        }
    } else {
        echo "$logo_path - " . (file_exists($logo_path) ? 'EXISTS' : 'MISSING') . "<br>";
        if (file_exists($logo_path)) {
            echo "<img src='$logo_path' style='max-height: 80px; border: 1px solid #ccc;' alt='Default Logo'><br>";
        }
    }
}

echo "<h3>3. Session Custom Logo Check</h3>";
$custom_logo_path = $_SESSION['custom_logo_path'] ?? null;
echo "Custom logo in session: " . ($custom_logo_path ?? 'NONE') . "<br>";

if ($custom_logo_path) {
    $full_custom_path = __DIR__ . '/' . ltrim($custom_logo_path, './');
    echo "Custom logo full path: $full_custom_path<br>";
    echo "Custom logo exists: " . (file_exists($full_custom_path) ? 'YES' : 'NO') . "<br>";
    if (file_exists($full_custom_path)) {
        echo "<img src='../$custom_logo_path' style='max-height: 100px; border: 1px solid #ccc;' alt='Custom Logo'><br>";
    }
}

echo "<h3>4. Certificate Logic Flow</h3>";
echo "<pre>";
echo "Certificate displays logo in this priority:\n";
echo "1. Custom uploaded logo (from session)\n";
echo "2. Site settings logo (from database)\n";
echo "3. Default icon (fallback)\n";
echo "</pre>";

echo "<h3>5. Current Certificate Display Logic</h3>";
echo "Based on your certificate files, here's what happens:<br>";

if ($custom_logo_path && file_exists(__DIR__ . '/' . ltrim($custom_logo_path, './'))) {
    echo "✅ USING CUSTOM LOGO: $custom_logo_path<br>";
} elseif ($logo_image && file_exists(__DIR__ . '/' . ltrim($logo_image, './'))) {
    echo "✅ USING SITE LOGO: $logo_image<br>";
} else {
    echo "⚠️ USING DEFAULT ICON (no custom or site logo found)<br>";
}

echo "<h3>6. How to Check What's Actually Displaying</h3>";
echo "<ol>";
echo "<li>Visit any certificate page</li>";
echo "<li>Look at the top-left corner where the logo appears</li>";
echo "<li>If you see an image, it's either your custom upload or site logo</li>";
echo "<li>If you see an icon (🎓 or similar), no logo file is available</li>";
echo "</ol>";

echo "<h3>7. Quick Actions</h3>";
echo "<a href='admin/site_settings.php' class='btn btn-primary'>Check Site Settings Logo</a> ";
echo "<a href='certificates/praise_certificate.php' class='btn btn-secondary'>View Certificate Example</a>";
?>
<style>
.btn { 
    display: inline-block; 
    padding: 8px 16px; 
    margin: 5px; 
    text-decoration: none; 
    border-radius: 4px; 
    color: white; 
}
.btn-primary { background-color: #007bff; }
.btn-secondary { background-color: #6c757d; }
</style>