<?php
namespace App\Controllers;

require_once BASE_PATH . '/app/helpers/conference_scope.php';
use App\Core\Database;

require BASE_PATH.'/app/helpers/DoiHelper.php';
require BASE_PATH.'/app/helpers/MailHelper.php';

class AdminDecisionEngine
{
    public static function evaluate($submission_id)
    {
        $db=Database::getInstance();
        $conf=$_SESSION['conference_id'];

        $avg=$db->query("
        SELECT AVG((originality+quality+relevance)/3)
        FROM review_scores rs
        JOIN assignments a ON rs.assignment_id=a.id
        WHERE a.submission_id=$submission_id
        ")->fetchColumn();

        $decision=$avg>=7?'accepted':($avg>=5?'revision':'rejected');

        $doi=null;
        if($decision==='accepted'){
            $doi=generate_doi($submission_id);
        }

        $db->prepare("UPDATE submissions SET decision=?,doi=? WHERE id=?")
           ->execute([$decision,$doi,$submission_id]);

        $stmt=$db->prepare("
        SELECT u.email FROM users u
        JOIN submissions s ON u.id=s.user_id
        WHERE s.id=?");
        $stmt->execute([$submission_id]);
        $email=$stmt->fetchColumn();

        if($email){
            send_mail_smtp($email,"Decision","Decision: ".$decision."\nDOI: ".$doi);
        }
    }
}
