<?php

namespace App\Services;

use App\Core\Database;

class PlatformInfinityService
{

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

$db=
Database::getInstance();

$queries=[

'runtime'=>
"
SELECT COUNT(*)
FROM platform_meta_runtime
WHERE org_id=?
",

'cohesive'=>
"
SELECT COUNT(*)
FROM platform_meta_runtime
WHERE org_id=?
AND runtime_score>=cohesion_score
",

'dashboard'=>
"
SELECT COUNT(*)
FROM platform_civilization_dashboard
WHERE conference_id=?
",

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

];

$out=[];

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

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

$stmt->execute([

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

]);

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

}

$out[
'unification'
]=

$out[
'dashboard'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'cohesive'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'unification'
]>=85

?

'unified'

:

'forming';

return $out;

}

}
