<?php

namespace App\Services;

use App\Core\Database;

class PlatformConstellationService
{

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

$db=
Database::getInstance();

$queries=[

'constellations'=>
"
SELECT COUNT(*)
FROM platform_constellation
WHERE org_id=?
",

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

'links'=>
"
SELECT COUNT(*)
FROM platform_ecosystem_graph
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==='links'
?
$conferenceId
:
$orgId

]);

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

}

$out[
'coordination'
]=

$out[
'constellations'
]

?

min(

100,

round(

(
$out[
'cohesive'
]
/
max(
1,
$out[
'constellations'
]
)

)
*
100

)

+

(
$out[
'links'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'coordination'
]>=85

?

'orchestrated'

:

'connecting';

return $out;

}

}
