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

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

$rows=
$db->query("
SELECT
reviewer_id,
conference_id,
COUNT(*) active_reviews
FROM review_assignments
WHERE status IN
(
'assigned',
'accepted',
'in_progress'
)
GROUP BY
reviewer_id,
conference_id
")->fetchAll(PDO::FETCH_ASSOC);

foreach($rows as $r){

    $active=
    (int)$r['active_reviews'];

    $forecast7=
    $active + ceil($active*0.50);

    $forecast14=
    $active + ceil($active*1.00);

    $status='healthy';

    if($forecast14 >= 12){
        $status='overloaded';
    }
    elseif($forecast14 >= 8){
        $status='high_load';
    }
    elseif($forecast14 >= 5){
        $status='busy';
    }

    $score=
    max(
        0,
        100-($forecast14*5)
    );

    $db->prepare("
    INSERT INTO reviewer_capacity_forecasts
    (
        conference_id,
        reviewer_id,
        current_active,
        forecast_7_days,
        forecast_14_days,
        forecast_status,
        forecast_score
    )
    VALUES
    (
        ?,?,?,?,?,?,?
    )
    ")->execute([
        $r['conference_id'],
        $r['reviewer_id'],
        $active,
        $forecast7,
        $forecast14,
        $status,
        $score
    ]);

}

echo "DONE\n";
