<?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 "REVIEWER REWARD INTELLIGENCE\n";

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

$rows=
$db->query("
SELECT
t.reviewer_id,
t.conference_id,
t.talent_score,
r.retention_score
FROM reviewer_talent_management t
LEFT JOIN reviewer_retention_intelligence r
ON t.reviewer_id=r.reviewer_id
AND t.conference_id=r.conference_id
")->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $r){

    $award='Participation Award';
    $reason='Active reviewer participation';

    $score=
    round(
        (
            (float)$r['talent_score']
            +
            (float)$r['retention_score']
        ) / 2,
        2
    );

    if($score>=85){

        $award='Reviewer of the Year';

        $reason=
        'Elite reviewer performance and retention';

    }
    elseif($score>=75){

        $award='Outstanding Leadership Award';

        $reason=
        'Leadership and reviewer excellence';

    }
    elseif($score>=50){

        $award='Emerging Leader Award';

        $reason=
        'Strong growth and leadership potential';

    }
    elseif($score>=40){

        $award='Excellence in Reviewing';

        $reason=
        'Consistent reviewer contribution';

    }

    $db->prepare("
    INSERT INTO reviewer_reward_intelligence
    (
        reviewer_id,
        conference_id,
        award_category,
        award_score,
        award_reason
    )
    VALUES
    (
        ?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['conference_id'],
        $award,
        $score,
        $reason
    ]);

}

echo "DONE\n";
