<?php

namespace App\Services;

use App\Core\Database;

class ReviewerLoadService
{
    public static function getReviewerLoad(
        int $reviewerId,
        int $conferenceId
    ): int
    {
        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT COUNT(*)
            FROM review_assignments
            WHERE reviewer_id = ?
            AND conference_id = ?
            AND status != 'completed'
        ");

        $stmt->execute([
            $reviewerId,
            $conferenceId
        ]);

        return (int)$stmt->fetchColumn();
    }

    public static function canAssign(
        int $reviewerId,
        int $conferenceId,
        int $maxLoad = 5
    ): bool
    {
        $load =
            self::getReviewerLoad(
                $reviewerId,
                $conferenceId
            );

        return $load < $maxLoad;
    }

    public static function alreadyAssigned(
        int $submissionId,
        int $reviewerId
    ): bool
    {
        $db = Database::getInstance();

        $stmt = $db->prepare("
            SELECT id
            FROM review_assignments
            WHERE submission_id = ?
            AND reviewer_id = ?
            LIMIT 1
        ");

        $stmt->execute([
            $submissionId,
            $reviewerId
        ]);

        return (bool)$stmt->fetchColumn();
    }
}
