<?php

namespace App\Services;

use App\Core\Database;

class PlatformOmegaService
{

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

$db=
Database::getInstance();

$queries=[

'engine'=>
"
SELECT COUNT(*)
FROM platform_endurance_engine
WHERE org_id=?
",

'stable'=>
"
SELECT COUNT(*)
FROM platform_endurance_engine
WHERE org_id=?
AND endurance_score>=continuity_score
",

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

'healthy'=>
"
SELECT COUNT(*)
FROM platform_continuity_observatory
WHERE conference_id=?
AND continuity_score>=80
"

];

$out=[];

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

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

$stmt->execute([

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

]);

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

}

$out[
'endurance'
]=

$out[
'observatory'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'stable'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'endurance'
]>=85

?

'enduring'

:

'preserving';

return $out;

}

}
