<?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;

final class ReviewerAssignmentsController extends Controller
{
    private function conferenceId(): int
    {
        return (int)(
            $_SESSION['conference_id']
            ??
            $_SESSION['current_conference_id']
            ??
            0
        );
    }

    public function index(): void
    {
        require_login();

        $db = Database::getInstance();

        $uid =
            (int)auth_id();

        $conferenceId =
            $this->conferenceId();

        $st = $db->prepare("
SELECT
ra.*,

s.submission_no,
COALESCE(
s.title,
s.title_en,
s.title_ar,
CONCAT('Submission #',s.id)
) title,

s.abstract,
s.status submission_status,

c.title_en conference_title_en,
c.title_ar conference_title_ar

FROM review_assignments ra

INNER JOIN submissions s
ON s.id=ra.submission_id

LEFT JOIN conferences c
ON c.id=s.conference_id

WHERE
ra.reviewer_id=:uid
AND s.conference_id=:conference_id

ORDER BY
FIELD(
ra.status,
'assigned',
'submitted',
'completed'
),
ra.id DESC
");

        $st->execute([
            ':uid' => $uid,
            ':conference_id' => $conferenceId
        ]);

        $rows =
            $st->fetchAll(
                \PDO::FETCH_ASSOC
            );

        $this->view(
            'reviewer/assignments',
            [
                'rows'=>$rows
            ]
        );
    }

    public function reviewForm(): void
    {
        require_login();

        $db =
            Database::getInstance();

        $uid =
            (int)auth_id();

        $assignmentId =
            (int)(
                $_GET['assignment_id']
                ?? 0
            );

        $conferenceId =
            $this->conferenceId();

        $st =
            $db->prepare("
SELECT
ra.*,

s.submission_no,
COALESCE(
s.title,
s.title_en,
s.title_ar
) title,

s.abstract,
s.conference_id,

c.title_en conference_title_en,
c.title_ar conference_title_ar

FROM review_assignments ra

INNER JOIN submissions s
ON s.id=ra.submission_id

LEFT JOIN conferences c
ON c.id=s.conference_id

WHERE
ra.id=:id
AND ra.reviewer_id=:uid
AND s.conference_id=:conference_id

LIMIT 1
");

        $st->execute([
            ':id'=>$assignmentId,
            ':uid'=>$uid,
            ':conference_id'=>$conferenceId
        ]);

        $a =
            $st->fetch();

        if (!$a) {

            http_response_code(
                404
            );

            exit(
                'Assignment not found'
            );
        }

        $rv =
            $db->prepare("
SELECT *
FROM reviews
WHERE assignment_id=:aid
LIMIT 1
");

        $rv->execute([
            ':aid'=>$assignmentId
        ]);

        $review =
            $rv->fetch()
            ?: null;

        $this->view(
            'reviewer/review_form',
            [
                'a'=>$a,
                'review'=>$review,
                'round'=>null
            ]
        );
    }

    public function submitReview(): void
    {
        require_login();

        redirect(
            'reviewer/assignments'
        );
    }
}
