<?php

namespace App\Controllers;

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

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

class ReviewFormController extends Controller
{
    private function requireAdmin()
    {
        if (!isset($_SESSION['user_id'])) {
            header("Location: /easychair/login");
            exit;
        }
    }

    /*
    =========================================
    LIST REVIEW FORM CRITERIA
    =========================================
    */

    public function index()
    {
        $this->requireAdmin();

        $db = Database::getInstance();

        $conference_id =
            $_SESSION['conference_id']
            ?? null;

        $stmt = $db->prepare("
            SELECT *
            FROM review_form_criteria
            WHERE
                conference_id = ?
            ORDER BY
                sort_order ASC,
                id ASC
        ");

        $stmt->execute([
            $conference_id
        ]);

        $criteria =
            $stmt->fetchAll(\PDO::FETCH_ASSOC);

        $this->render_admin(
            'review_form/index',
            [
                'title' => 'Review Form',
                'criteria' => $criteria
            ]
        );
    }

    /*
    =========================================
    SAVE CRITERION
    =========================================
    */

    public function save()
    {
        $this->requireAdmin();

        $db =
            Database::getInstance();

        $id =
            $_POST['id']
            ?? null;

        $conference_id =
            $_SESSION['conference_id'];

        $name =
            trim(
                $_POST['criterion_name']
                ?? ''
            );

        $max_score =
            intval(
                $_POST['max_score']
                ?? 10
            );

        $sort_order =
            intval(
                $_POST['sort_order']
                ?? 1
            );

        $is_active =
            isset($_POST['is_active'])
            ? 1
            : 0;

        if (!$name) {
            die("Criterion name required.");
        }

        /*
        UPDATE
        */

        if ($id) {

            $stmt = $db->prepare("
                UPDATE
                    review_form_criteria
                SET
                    criterion_name = ?,
                    max_score = ?,
                    sort_order = ?,
                    is_active = ?
                WHERE
                    id = ?
            ");

            $stmt->execute([
                $name,
                $max_score,
                $sort_order,
                $is_active,
                $id
            ]);

        }

        /*
        INSERT
        */

        else {

            $stmt = $db->prepare("
                INSERT INTO
                    review_form_criteria
                (
                    conference_id,
                    criterion_name,
                    max_score,
                    sort_order,
                    is_active,
                    created_at
                )
                VALUES
                (
                    ?,
                    ?,
                    ?,
                    ?,
                    ?,
                    NOW()
                )
            ");

            $stmt->execute([
                $conference_id,
                $name,
                $max_score,
                $sort_order,
                $is_active
            ]);

        }

        header(
            "Location: /easychair/admin/review-form"
        );

        exit;
    }
}
