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

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

$bestConference=
$db->query("
SELECT conference_id,overall_score
FROM conference_portfolio_intelligence
ORDER BY portfolio_rank
LIMIT 1
")->fetch(PDO::FETCH_ASSOC);

$worstConference=
$db->query("
SELECT conference_id,overall_score
FROM conference_portfolio_intelligence
ORDER BY portfolio_rank DESC
LIMIT 1
")->fetch(PDO::FETCH_ASSOC);

$bestOrg=
$db->query("
SELECT org_id,governance_score
FROM organization_governance_intelligence
ORDER BY governance_score DESC
LIMIT 1
")->fetch(PDO::FETCH_ASSOC);

$riskCount=
(int)$db->query("
SELECT COUNT(*)
FROM conference_portfolio_intelligence
WHERE risk_level='High'
")->fetchColumn();

$pendingActions=
(int)$db->query("
SELECT COUNT(*)
FROM orchestration_action_approvals
WHERE approval_status='pending'
")->fetchColumn();

$pendingHealing=
(int)$db->query("
SELECT COUNT(*)
FROM self_healing_actions
WHERE healing_status='pending'
")->fetchColumn();

$summary=
"Portfolio Leader Conference #".
($bestConference['conference_id'] ?? '-') .
". Best Organization #".
($bestOrg['org_id'] ?? '-') .
". High Risk Conferences=".$riskCount;

$risks=
"High Risk Conferences=".$riskCount.
". Pending Actions=".$pendingActions.
". Pending Healing=".$pendingHealing;

$opportunities=
"Best Conference #".
($bestConference['conference_id'] ?? '-') .
". Best Organization #".
($bestOrg['org_id'] ?? '-');

$actions=
"Prioritize intervention for Conference #".
($worstConference['conference_id'] ?? '-') .
". Reduce pending actions and healing backlog.";

$db->prepare("
INSERT INTO executive_briefings
(
briefing_date,
summary_text,
top_risks,
top_opportunities,
recommended_actions
)
VALUES
(
CURDATE(),
?,?,?,?
)
")->execute([
$summary,
$risks,
$opportunities,
$actions
]);

echo "DONE\n";
