<?php

namespace App\Services;

use App\Core\Database;

class PlatformApexService
{

public static function dashboard(
int $orgId,
int $conferenceId
): array
{

$db=
Database::getInstance();

$queries=[

'mesh'=>
"
SELECT COUNT(*)
FROM platform_command_mesh
WHERE org_id=?
",

'ready'=>
"
SELECT COUNT(*)
FROM platform_command_mesh
WHERE org_id=?
AND authority_score>=execution_score
",

'observatory'=>
"
SELECT COUNT(*)
FROM platform_executive_observatory
WHERE conference_id=?
",

'aligned'=>
"
SELECT COUNT(*)
FROM platform_executive_observatory
WHERE conference_id=?
AND executive_score>=80
"

];

$out=[];

foreach(
$queries
as $k=>$sql
){

$stmt=
$db->prepare(
$sql
);

$stmt->execute([

in_array(
$k,
[
'observatory',
'aligned'
]
)
?
$conferenceId
:
$orgId

]);

$out[$k]=
(int)
$stmt->fetchColumn();

}

$out[
'execution'
]=

$out[
'observatory'
]

?

min(

100,

round(

(
$out[
'aligned'
]
/
max(
1,
$out[
'observatory'
]
)

)
*
100

)

+

(
$out[
'ready'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'execution'
]>=85

?

'executive'

:

'coordinating';

return $out;

}

}
