<?php
namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/conference_scope.php';

use App\Core\Database;

class CertificateController
{
    public function verify()
    {
        $token = trim($_GET['token'] ?? '');

        if ($token === '') {
            http_response_code(404);
            exit("Certificate token is required.");
        }

        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT cert.*, c.short_name, c.title_ar AS conference_title_ar, c.title_en AS conference_title_en
            FROM conference_certificates cert
            LEFT JOIN conferences c ON c.id = cert.conference_id
            WHERE cert.verify_token = ?
            LIMIT 1
        ");
        $stmt->execute([$token]);
        $certificate = $stmt->fetch(\PDO::FETCH_ASSOC);

        if (!$certificate) {
            http_response_code(404);
            exit("Certificate not found.");
        }

        require BASE_PATH . '/app/views/certificates/verify.php';
    }

    public function print()
    {
        $token = trim($_GET['token'] ?? '');

        if ($token === '') {
            http_response_code(404);
            exit("Certificate token is required.");
        }

        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT cert.*, c.short_name, c.title_ar AS conference_title_ar, c.title_en AS conference_title_en
            FROM conference_certificates cert
            LEFT JOIN conferences c ON c.id = cert.conference_id
            WHERE cert.verify_token = ?
            LIMIT 1
        ");
        $stmt->execute([$token]);
        $certificate = $stmt->fetch(\PDO::FETCH_ASSOC);

        if (!$certificate) {
            http_response_code(404);
            exit("Certificate not found.");
        }

        require BASE_PATH . '/app/views/certificates/print.php';
    }
}
