<?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 DIGITAL TWINS\n";

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

$rows=
$db->query("
SELECT
reviewer_id,
conference_id,
expert_score,
expert_level
FROM reviewer_global_experts
ORDER BY expert_score DESC
")->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $r){

    $score=(float)$r['expert_score'];

    $growth=
    round(
        min(99,$score*1.05),
        2
    );

    $leadership=
    round(
        min(99,$score*1.02),
        2
    );

    $promotion=
    round(
        min(99,$score*1.08),
        2
    );

    $futureRole='Reviewer';

    if($score>=85){
        $futureRole='Future Conference Chair';
    }
    elseif($score>=60){
        $futureRole='Future Program Chair';
    }
    elseif($score>=40){
        $futureRole='Future Track Chair';
    }

    $burnout='Low';

    if($score>=80){
        $burnout='Medium';
    }

    $retention='Stable';

    if($score<20){
        $retention='At Risk';
    }

    $db->prepare("
    INSERT INTO reviewer_digital_twins
    (
        reviewer_id,
        conference_id,
        digital_twin_score,
        growth_potential,
        leadership_probability,
        promotion_probability,
        burnout_forecast,
        retention_forecast,
        future_role
    )
    VALUES
    (
        ?,?,?,?,?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['conference_id'],
        $score,
        $growth,
        $leadership,
        $promotion,
        $burnout,
        $retention,
        $futureRole
    ]);

}

echo 'DONE';
