<?php
declare(strict_types=1);

namespace App\Controllers;

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

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

use App\Core\Controller;
use App\Core\Database;
use App\Models\OperationsModel;
use App\Services\MailTemplateService;

final class CommunicationsController extends Controller
{
    public function queueIndex(): void
    {
        require_permission('email_queue.view');

        $db = Database::getInstance();
        $rows = $db->query("SELECT * FROM email_queue ORDER BY id DESC LIMIT 200")->fetchAll();

        $this->view('admin/email_queue/index', [
            'rows' => $rows,
        ]);
    }

    public function sendPending(): void
    {
        require_permission('email_queue.manage');

        if ($this->isPost()) {
            csrf_check();
            $count = MailTemplateService::sendPending(50);
            flash('success', trans('sent') . ' ' . $count);
        }

        redirect('admin/email-queue');
    }

    public function reviewerCertificateUi(): void
    {
        require_permission('conference_certificates.print');

        $reviewerId = (int) ($_GET['reviewer_id'] ?? 0);
        $conferenceId = (int)
\request_conference_id();

        $model = new OperationsModel();
        if (!$model->reviewerCertificateEligible($reviewerId, $conferenceId)) {
            http_response_code(403);
            exit('Reviewer certificate not eligible');
        }

        $db = Database::getInstance();
        $stmt = $db->prepare("
            SELECT u.*, c.title_en AS conference_title_en, c.title_ar AS conference_title_ar
            FROM users u, conferences c
            WHERE u.id = :reviewer_id AND c.id = :conference_id
            LIMIT 1
        ");
        $stmt->execute([
            ':reviewer_id' => $reviewerId,
            ':conference_id' => $conferenceId,
        ]);
        $row = $stmt->fetch();
        if (!$row) {
            http_response_code(404);
            exit('Not found');
        }

        $reviewer = $row;
        $conference = [
            'title_en' => $row['conference_title_en'] ?? '',
            'title_ar' => $row['conference_title_ar'] ?? '',
        ];

        require __DIR__ . '/../views/print/reviewer_certificate.php';
    }

    public function tpcCertificateUi(): void
    {
        require_permission('conference_certificates.print');

        $conferenceId = (int)
\request_conference_id();
        $email = trim((string) ($_GET['email'] ?? ''));

        $model = new OperationsModel();
        if (!$model->tpcCertificateEligible($email, $conferenceId)) {
            http_response_code(403);
            exit('TPC certificate not eligible');
        }

        $conference = null;
        foreach ($model->conferences() as $c) {
            if ((int) $c['id'] === $conferenceId) {
                $conference = $c;
                break;
            }
        }

        $db = Database::getInstance();
        $stmt = $db->prepare("
            SELECT *
            FROM conference_committees
            WHERE conference_id = :conference_id
              AND committee_type = 'tpc'
              AND email = :email
            LIMIT 1
        ");
        $stmt->execute([
            ':conference_id' => $conferenceId,
            ':email' => $email,
        ]);
        $member = $stmt->fetch();
        if (!$member) {
            http_response_code(404);
            exit('TPC member not found');
        }

        require __DIR__ . '/../views/print/tpc_certificate.php';
    }
}
