<?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 "LEADERSHIP SUCCESSION AI\n";

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

$rows=
$db->query("
SELECT
reviewer_id,
conference_id,
reputation_score,
trust_index,
reputation_tier
FROM reviewer_reputation_network
ORDER BY reputation_score DESC
")->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $r){

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

    $role='Leadership Candidate';
    $tier='Developing';

    if($score>=85){

        $role='Conference Chair Successor';
        $tier='Executive';

    }
    elseif($score>=60){

        $role='Program Chair Successor';
        $tier='Senior';

    }
    elseif($score>=40){

        $role='Track Chair Successor';
        $tier='Advanced';

    }

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

    $db->prepare("
    INSERT INTO reviewer_leadership_succession_ai
    (
        reviewer_id,
        conference_id,
        succession_role,
        readiness_score,
        succession_confidence,
        leadership_tier,
        recommendation_text
    )
    VALUES
    (
        ?,?,?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['conference_id'],
        $role,
        $score,
        $confidence,
        $tier,
        'Autonomous leadership succession recommendation'
    ]);

}

echo "DONE\n";
