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

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

$confs=
$db->query("
SELECT id
FROM conferences
")->fetchAll(PDO::FETCH_ASSOC);

foreach($confs as $c){

    $cid=(int)$c['id'];

    $health=
    (float)$db->query("
    SELECT
    COALESCE(MAX(overall_health_score),0)
    FROM conference_health_metrics
    WHERE conference_id=".$cid
    )->fetchColumn();

    if($health < 60){

        $db->prepare("
        INSERT INTO
        predictive_conference_intelligence
        (
            conference_id,
            prediction_type,
            prediction_score,
            prediction_text,
            predicted_date
        )
        VALUES
        (
            ?,?,?,?,?
        )
        ")->execute([
            $cid,
            'conference_health_decline',
            100-$health,
            'Conference health likely to decline',
            date('Y-m-d',strtotime('+30 days'))
        ]);

    }

    $risk=
    (int)$db->query("
    SELECT COUNT(*)
    FROM reviewer_risk_flags
    WHERE conference_id=".$cid."
    AND is_active=1
    ")->fetchColumn();

    if($risk > 5){

        $db->prepare("
        INSERT INTO
        predictive_conference_intelligence
        (
            conference_id,
            prediction_type,
            prediction_score,
            prediction_text,
            predicted_date
        )
        VALUES
        (
            ?,?,?,?,?
        )
        ")->execute([
            $cid,
            'reviewer_failure_risk',
            $risk,
            'Potential reviewer deadline failures',
            date('Y-m-d',strtotime('+14 days'))
        ]);

    }

}

echo "DONE\n";
