<?php
// admin/test-cert.php - Test Certificate Generation
require_once '../includes/config.php';
require_once '../includes/check-certificate.php';

echo "<h2>Certificate Test</h2>";

// Test with user_id=1 and course_id=1 (change as needed)
$user_id = 1;
$course_id = 1;

$result = checkAndIssueCertificate($pdo, $user_id, $course_id);

if($result) {
    echo "<p style='color:green'>✅ Certificate issued! ID: $result</p>";
    
    // Show the certificate
    $stmt = $pdo->prepare("SELECT * FROM certificates WHERE id = ?");
    $stmt->execute([$result]);
    $cert = $stmt->fetch();
    echo "<p>Number: {$cert['certificate_number']}</p>";
    echo "<p>Student: {$cert['student_name']}</p>";
    echo "<p>Course: {$cert['course_name']}</p>";
    echo "<a href='view-certificate.php?id={$result}' target='_blank'>View Certificate</a>";
} else {
    echo "<p style='color:orange'>⚠️ Certificate not issued. Check:</p>";
    echo "<ul>";
    
    // Check if already has cert
    $stmt = $pdo->prepare("SELECT id FROM certificates WHERE user_id=? AND course_id=?");
    $stmt->execute([$user_id, $course_id]);
    if($stmt->fetch()) echo "<li>Already has certificate</li>";
    
    // Check lesson progress
    $stmt = $pdo->prepare("
        SELECT COUNT(l.id) as total, 
               COUNT(CASE WHEN lp.completed=1 THEN 1 END) as completed
        FROM lessons l 
        LEFT JOIN lesson_progress lp ON l.id=lp.lesson_id AND lp.user_id=? 
        WHERE l.course_id=?
    ");
    $stmt->execute([$user_id, $course_id]);
    $p = $stmt->fetch();
    echo "<li>Lessons: {$p['completed']}/{$p['total']} completed</li>";
    echo "</ul>";
}

echo "<p style='color:red'>DELETE THIS FILE AFTER TESTING!</p>";
?>