<?php

namespace App\Services;

use App\Core\Database;

class PlatformNexusPrimeService
{

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

$db=
Database::getInstance();

$queries=[

'kernels'=>
"
SELECT COUNT(*)
FROM platform_operating_kernel
WHERE org_id=?
",

'healthy'=>
"
SELECT COUNT(*)
FROM platform_operating_kernel
WHERE org_id=?
AND kernel_score>=health_score
",

'fabrics'=>
"
SELECT COUNT(*)
FROM platform_intelligence_fabric
WHERE conference_id=?
",

'cohesive'=>
"
SELECT COUNT(*)
FROM platform_intelligence_fabric
WHERE conference_id=?
AND cohesion_score>=80
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'fabrics',
'cohesive'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'unification'
]=

$out[
'fabrics'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'healthy'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'unification'
]>=85

?

'unified'

:

'forming';

return $out;

}

}
