<?php

namespace App\Controllers;

class CertificateController {
    private $pdo;
    private $config;
    
    public function __construct($pdo, $config) {
        $this->pdo = $pdo;
        $this->config = $config;
    }
    
    public function verify() {
        $result = null;
        $error = '';
        
        if ($_SERVER['REQUEST_METHOD'] === 'POST') {
            $number = trim($_POST['certificate_number'] ?? '');
            if (empty($number)) {
                $error = 'Please enter a certificate number.';
            } else {
                $stmt = $this->pdo->prepare("SELECT c.*, s.student_number, u.first_name, u.last_name, cr.title as course_title FROM certificates c JOIN students s ON c.student_id = s.id JOIN users u ON s.user_id = u.id JOIN courses cr ON c.course_id = cr.id WHERE c.certificate_number = ? AND c.status = 'issued'");
                $stmt->execute([$number]);
                $result = $stmt->fetch();
                
                if (!$result) {
                    $error = 'Certificate not found or invalid.';
                }
            }
        }
        
        $theme = get_setting('default_theme', 'orbit-dark');
        include __DIR__ . '/../Views/layouts/public.php';
        include __DIR__ . '/../Views/certificates/verify.php';
        include __DIR__ . '/../Views/layouts/footer.php';
    }
}
