<?php

namespace App\Services;

use App\Core\Database;

class PlatformOrbitService
{

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

$db=
Database::getInstance();

$queries=[

'orbits'=>
"
SELECT COUNT(*)
FROM platform_orbits
WHERE org_id=?
",

'stable'=>
"
SELECT COUNT(*)
FROM platform_orbits
WHERE org_id=?
AND orbit_score>=80
",

'interactions'=>
"
SELECT COUNT(*)
FROM platform_interaction_model
WHERE conference_id=?
",

'capabilities'=>
"
SELECT COUNT(*)
FROM platform_capabilities
WHERE org_id=?
"

];

$out=[];

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

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

$stmt->execute([

$k==='interactions'
?
$conferenceId
:
$orgId

]);

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

}

$out[
'coordination'
]=

$out[
'orbits'
]

?

min(

100,

round(

(
$out[
'stable'
]
/
max(
1,
$out[
'orbits'
]
)

)
*
100

)

+

(
$out[
'interactions'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'coordination'
]>=85

?

'synchronized'

:

'coordinating';

return $out;

}

}
