<?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 REPUTATION NETWORK\n";

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

$rows=
$db->query("
SELECT
reviewer_id,
home_conference_id,
marketplace_score
FROM reviewer_marketplace
ORDER BY marketplace_score DESC
")->fetchAll(PDO::FETCH_ASSOC);

$rank=1;

foreach($rows as $r){

    $score=
    round(
        (float)$r['marketplace_score'],
        2
    );

    $trust=
    round(
        $score*1.10,
        2
    );

    $tier='Standard Reviewer';
    $status='Active';

    if($score>=80){

        $tier='Global Distinguished Reviewer';

    }
    elseif($score>=40){

        $tier='Senior Trusted Reviewer';

    }
    elseif($score>=20){

        $tier='Emerging Reputation Leader';

    }
    elseif($score<10){

        $tier='Reputation Recovery Program';
        $status='Needs Improvement';

    }

    $db->prepare("
    INSERT INTO reviewer_reputation_network
    (
        reviewer_id,
        conference_id,
        reputation_score,
        trust_index,
        global_rank,
        reputation_tier,
        reputation_status
    )
    VALUES
    (
        ?,?,?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['home_conference_id'],
        $score,
        $trust,
        $rank,
        $tier,
        $status
    ]);

    $rank++;

}

echo 'DONE';
