<?php

namespace App\Services;

use App\Core\Database;

class PlatformUniverseService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'systems'=>

"

SELECT COUNT(*)

FROM platform_federated_systems

WHERE org_id=?

",

'sessions'=>

"

SELECT COUNT(*)

FROM platform_federation_sessions

WHERE conference_id=?

",

'events'=>

"

SELECT COUNT(*)

FROM platform_event_bus

WHERE conference_id=?

",

'memory'=>

"

SELECT COUNT(*)

FROM platform_memory

WHERE conference_id=?

"

];

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

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

if(
$k==='systems'
){

$stmt->execute([
$orgId
]);

}
else{

$stmt->execute([
$conferenceId
]);

}

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

}

$out[
'federation'
]=

min(

100,

55+

(
$out[
'systems'
]*8
)

+

(
$out[
'sessions'
]*5
)

);

$out[
'state'
]=

$out[
'federation'
]>=85

?

'federated'

:

'local';

return
$out;

}

}
