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

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

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

$conferences=
$db->query("
SELECT id
FROM conferences
WHERE is_active=1
")->fetchAll(PDO::FETCH_COLUMN);

foreach($reviewers as $r){

    foreach($conferences as $target){

        if(
            (int)$target
            ==
            (int)$r['home_conference_id']
        ){
            continue;
        }

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

        $type='Reviewer Exchange';

        if($score>=80){
            $type='Elite Reviewer Exchange';
        }
        elseif($score>=50){
            $type='Leadership Exchange';
        }

        $db->prepare("
        INSERT INTO reviewer_exchange_recommendations
        (
            reviewer_id,
            source_conference_id,
            target_conference_id,
            exchange_score,
            exchange_type,
            recommendation_text
        )
        VALUES
        (
            ?,?,?,?,?,?
        )
        ")->execute([
            $r['reviewer_id'],
            $r['home_conference_id'],
            $target,
            $score,
            $type,
            'Recommended cross-conference reviewer sharing'
        ]);

    }

}

echo "DONE\n";
