<?php

define(
'BASE_PATH',
realpath(__DIR__.'/../..')
);

require_once BASE_PATH.'/config/autoload.php';
require_once BASE_PATH.'/config/bootstrap.php';

$db=\App\Core\Database::getInstance();

echo "RECOMMENDATION EXPLANATIONS\n";

$db->exec(
"TRUNCATE TABLE reviewer_recommendation_explanations"
);

$rows=
$db->query("
SELECT *
FROM reviewer_assignment_scores
")->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $r){

    $reasons=[];

    if(
        (float)$r['expertise_score'] >= 70
    ){
        $reasons[]='High expertise';
    }

    if(
        (float)$r['performance_score'] >= 60
    ){
        $reasons[]='Strong reviewer performance';
    }

    if(
        (float)$r['capacity_score'] >= 60
    ){
        $reasons[]='Available workload capacity';
    }

    if(
        (float)$r['deadline_score'] >= 70
    ){
        $reasons[]='Reliable completion history';
    }

    if(
        (float)$r['risk_penalty'] <= 0
    ){
        $reasons[]='No major reviewer risk';
    }

    if(
        (float)$r['workload_penalty'] <= 0
    ){
        $reasons[]='Not overloaded';
    }

    $text=
    implode(
        ', ',
        $reasons
    );

    $db->prepare("
    INSERT INTO
    reviewer_recommendation_explanations
    (
        conference_id,
        reviewer_id,
        final_assignment_score,
        explanation_text
    )
    VALUES
    (
        ?,?,?,?
    )
    ")->execute([
        $r['conference_id'],
        $r['reviewer_id'],
        $r['final_assignment_score'],
        $text
    ]);

}

echo "DONE\n";
