<?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 "GLOBAL EXPERT DISCOVERY\n";

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

$rows=
$db->query("
SELECT
reviewer_id,
conference_id,
readiness_score,
succession_confidence
FROM reviewer_leadership_succession_ai
ORDER BY readiness_score DESC
")->fetchAll(PDO::FETCH_ASSOC);

$rank=1;

foreach($rows as $r){

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

    $level='Emerging Expert';

    if($score>=85){

        $level='Global Expert';

    }
    elseif($score>=60){

        $level='Strategic Expert';

    }
    elseif($score>=40){

        $level='Domain Expert';

    }

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

    $db->prepare("
    INSERT INTO reviewer_global_experts
    (
        reviewer_id,
        conference_id,
        expert_score,
        expert_rank,
        expert_level,
        discovery_confidence,
        recommendation_text
    )
    VALUES
    (
        ?,?,?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['conference_id'],
        $score,
        $rank,
        $level,
        $confidence,
        'Autonomous expert discovery recommendation'
    ]);

    $rank++;

}

echo "DONE\n";
