<?php
declare(strict_types=1);

namespace App\Controllers;

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

use App\Core\Controller;
use App\Core\Database;

final class PublicCertificateController extends Controller
{
    public function show(): void
    {
        $db = Database::getInstance();
        $code = trim((string)($_GET['code'] ?? ''));

        if ($code === '') { http_response_code(400); exit('Missing code'); }

        $st = $db->prepare("
          SELECT cert.*, u.first_name,u.last_name,u.email,
                 c.title_en,c.title_ar
          FROM certificates cert
          INNER JOIN users u ON u.id=cert.user_id
          INNER JOIN conferences c ON c.id=cert.conference_id
          WHERE cert.cert_code=:code
          LIMIT 1
        ");
        $st->execute([':code'=>$code]);
        $row = $st->fetch();
        if (!$row) { http_response_code(404); exit('Not found'); }

        $this->view('public/certificates/show', ['row'=>$row]);
    }
}
