<?php
declare(strict_types=1);

namespace App\Controllers;

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

use App\Core\Controller;
use App\Models\Submission;
use App\Models\Review;

final class BadgeController extends Controller
{
    public function printBadge(): void
    {
        require_permission('badges.generate');

        $type = trim((string) ($_GET['type'] ?? 'author'));

        if ($type === 'author') {
            $submissionId = (int) ($_GET['submission_id'] ?? 0);
            $submission = (new Submission())->findById($submissionId);
            if (!$submission) {
                http_response_code(404);
                exit('Submission not found');
            }
            require __DIR__ . '/../views/badges/author.php';
            return;
        }

        if ($type === 'reviewer') {
            $reviewerId = (int) ($_GET['reviewer_id'] ?? 0);
            $rows = (new Review())->assignmentsForReviewer($reviewerId);
            $reviewer = !empty($rows[0]) ? $rows[0] : null;
            if (!$reviewer) {
                http_response_code(404);
                exit('Reviewer not found');
            }
            require __DIR__ . '/../views/badges/reviewer.php';
            return;
        }

        http_response_code(400);
        exit('Invalid badge type');
    }
}
