<?php
require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../classes/Course.php';

requireLogin();
requireAnyRole(['instructor']);
$user = getCurrentUser();
$db = Database::getInstance();

// Revoke certificate
if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_POST['action']) && $_POST['action'] === 'revoke' && !empty($_POST['id'])) {
    $id = (int)$_POST['id'];
    // Ensure certificate is for instructor's course
    $c = $db->get("SELECT cert.* FROM certificates cert JOIN courses c ON cert.course_id = c.id WHERE cert.id = ? AND c.instructor_id = ?", [$id, $user['id']]);
    if ($c) {
        $db->update('certificates', ['is_revoked' => 1], 'id = ?', [$id]);
        setFlashMessage('success', 'Certificate revoked.');
    } else {
        setFlashMessage('error', 'Certificate not found or permission denied.');
    }
    header('Location: ' . APP_URL . '/lms/certificates_manage.php');
    exit;
}

$certificates = $db->getAll("SELECT cert.*, c.title as course_title, u.first_name, u.last_name FROM certificates cert JOIN courses c ON cert.course_id = c.id JOIN users u ON cert.student_id = u.id WHERE c.instructor_id = ? ORDER BY cert.issue_date DESC", [$user['id']]);

$pageTitle = 'Certificates';
include __DIR__ . '/includes/header.php';
?>

<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="m-0">Certificates</h5>
        <a href="<?php echo APP_URL; ?>/lms/dashboards/instructor.php" class="btn btn-outline-secondary btn-sm">Back</a>
    </div>
    <div class="card-body">
        <?php if (empty($certificates)): ?>
            <p class="text-muted">No certificates found for your courses.</p>
        <?php else: ?>
            <table class="table table-striped">
                <thead>
                    <tr><th>#</th><th>Student</th><th>Course</th><th>Issued</th><th>Action</th></tr>
                </thead>
                <tbody>
                    <?php foreach ($certificates as $c): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($c['certificate_number']); ?></td>
                            <td><?php echo htmlspecialchars($c['first_name'] . ' ' . $c['last_name']); ?></td>
                            <td><?php echo htmlspecialchars($c['course_title']); ?></td>
                            <td><?php echo htmlspecialchars($c['issue_date']); ?></td>
                            <td>
                                <?php if (empty($c['is_revoked'])): ?>
                                    <form method="post" onsubmit="return confirm('Revoke this certificate?');" class="d-inline">
                                        <input type="hidden" name="id" value="<?php echo (int)$c['id']; ?>">
                                        <button name="action" value="revoke" class="btn btn-sm btn-outline-danger">Revoke</button>
                                    </form>
                                <?php else: ?>
                                    <span class="text-muted">Revoked</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </div>
</div>

<?php include __DIR__ . '/includes/footer.php';
