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

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

$rows=
$db->query("
SELECT
reviewer_id,
conference_id,
talent_score,
leadership_score,
pipeline_role
FROM reviewer_leadership_pipeline
")->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $r){

    $role='Reviewer Successor';

    if(
        $r['pipeline_role']
        ==
        'Future Conference Chair'
    ){
        $role='Conference Chair Successor';
    }
    elseif(
        $r['pipeline_role']
        ==
        'Future Track Chair'
    ){
        $role='Track Chair Successor';
    }
    elseif(
        $r['pipeline_role']
        ==
        'Senior Reviewer'
    ){
        $role='Mentor Reviewer';
    }
    elseif(
        $r['pipeline_role']
        ==
        'Recovery Candidate'
    ){
        $role='Recovery Program';
    }

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

    $recommendation=
    'Continue succession monitoring';

    if(
        $role
        ==
        'Conference Chair Successor'
    ){
        $recommendation=
        'Prepare for conference chair responsibilities';
    }

    if(
        $role
        ==
        'Track Chair Successor'
    ){
        $recommendation=
        'Assign track leadership opportunities';
    }

    if(
        $role
        ==
        'Mentor Reviewer'
    ){
        $recommendation=
        'Assign reviewer mentoring duties';
    }

    if(
        $role
        ==
        'Recovery Program'
    ){
        $recommendation=
        'Enroll in reviewer recovery program';
    }

    $db->prepare("
    INSERT INTO reviewer_succession_plans
    (
        reviewer_id,
        conference_id,
        succession_role,
        readiness_score,
        recommendation_text
    )
    VALUES
    (
        ?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['conference_id'],
        $role,
        $score,
        $recommendation
    ]);

}

echo 'DONE';
