<?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 "AUTONOMOUS REVIEWER ECOSYSTEM\n";

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

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

foreach($rows as $r){

    $action='Monitor Reviewer';
    $priority='Low';
    $confidence=50;
    $reason='Normal reviewer monitoring';

    if($r['marketplace_score']>=80){

        $action='Promote To Conference Chair Candidate';
        $priority='Critical';
        $confidence=98;

        $reason=
        'Elite marketplace score and lifetime impact';

    }
    elseif($r['marketplace_score']>=60){

        $action='Assign Leadership Responsibilities';
        $priority='High';
        $confidence=90;

        $reason=
        'Strong leadership and reviewer history';

    }
    elseif($r['marketplace_score']>=40){

        $action='Assign Mentor Duties';
        $priority='Medium';
        $confidence=80;

        $reason=
        'Suitable mentor candidate';

    }
    elseif($r['marketplace_score']<10){

        $action='Enroll Recovery Program';
        $priority='High';
        $confidence=95;

        $reason=
        'Critical reviewer development required';

    }

    $db->prepare("
    INSERT INTO reviewer_ecosystem_actions
    (
        reviewer_id,
        conference_id,
        action_type,
        priority_level,
        confidence_score,
        action_reason
    )
    VALUES
    (
        ?,?,?,?,?,?
    )
    ")->execute([
        $r['reviewer_id'],
        $r['home_conference_id'],
        $action,
        $priority,
        $confidence,
        $reason
    ]);

}

echo "DONE\n";
