<?php
require_once 'config/database.php';

echo "<h2>Student Photo Debug Information</h2>";

try {
    // Check if photo column exists
    echo "<h3>1. Checking Photo Column:</h3>";
    $stmt = $pdo->query("SHOW COLUMNS FROM students LIKE 'photo'");
    $photo_column = $stmt->fetch();
    
    if ($photo_column) {
        echo "✅ Photo column exists: " . $photo_column['Field'] . " (" . $photo_column['Type'] . ")<br>";
    } else {
        echo "❌ Photo column does not exist<br>";
        exit;
    }
    
    // Check how many students have photos
    echo "<h3>2. Students with Photos:</h3>";
    $stmt = $pdo->query("SELECT COUNT(*) as count FROM students WHERE photo IS NOT NULL AND photo != ''");
    $count = $stmt->fetch()['count'];
    echo "Students with photo data: " . $count . "<br>";
    
    if ($count > 0) {
        echo "<h3>3. Sample Students with Photos:</h3>";
        $stmt = $pdo->query("SELECT id, student_id, first_name, last_name, photo FROM students WHERE photo IS NOT NULL AND photo != '' LIMIT 5");
        $students = $stmt->fetchAll();
        
        foreach ($students as $student) {
            echo "ID: " . $student['id'] . " | ";
            echo "Student ID: " . $student['student_id'] . " | ";
            echo "Name: " . $student['first_name'] . " " . $student['last_name'] . " | ";
            echo "Photo: " . htmlspecialchars($student['photo']) . "<br>";
        }
    }
    
    // Check uploads directory
    echo "<h3>4. Uploads Directory Status:</h3>";
    $upload_dir = 'uploads/students/';
    
    if (is_dir($upload_dir)) {
        echo "✅ Directory exists: " . $upload_dir . "<br>";
        
        // Count image files
        $image_files = glob($upload_dir . '*.{jpg,jpeg,png,gif,JPG,JPEG,PNG,GIF}', GLOB_BRACE);
        echo "Image files found: " . count($image_files) . "<br>";
        
        if (!empty($image_files)) {
            echo "Sample files:<br>";
            foreach (array_slice($image_files, 0, 5) as $file) {
                $filename = basename($file);
                echo "- " . $filename . " (" . filesize($file) . " bytes)<br>";
            }
        }
    } else {
        echo "❌ Directory does not exist: " . $upload_dir . "<br>";
        
        // Try to create the directory
        if (mkdir($upload_dir, 0755, true)) {
            echo "✅ Created directory: " . $upload_dir . "<br>";
        } else {
            echo "❌ Failed to create directory<br>";
        }
    }
    
    // Test specific file access
    echo "<h3>5. File Access Test:</h3>";
    if (!empty($image_files)) {
        $test_file = $image_files[0];
        echo "Testing file: " . $test_file . "<br>";
        echo "File exists: " . (file_exists($test_file) ? "✅ Yes" : "❌ No") . "<br>";
        echo "File readable: " . (is_readable($test_file) ? "✅ Yes" : "❌ No") . "<br>";
        echo "File size: " . filesize($test_file) . " bytes<br>";
        
        // Test web access
        $web_path = str_replace('\\', '/', $test_file);
        echo "Web path test: <a href='" . $web_path . "' target='_blank'>" . $web_path . "</a><br>";
    }
    
    // Check permissions
    echo "<h3>6. Permissions Check:</h3>";
    echo "Script is running as: " . get_current_user() . "<br>";
    echo "Upload directory writable: " . (is_writable($upload_dir) ? "✅ Yes" : "❌ No") . "<br>";
    
} catch (Exception $e) {
    echo "❌ Error: " . $e->getMessage() . "<br>";
}
?>