<?php

namespace App\Services;

use App\Core\Database;

class PlatformSingularityService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'knowledge'=>

"

SELECT COUNT(*)
FROM platform_runtime_knowledge
WHERE conference_id=?

",

'planes'=>

"

SELECT COUNT(*)
FROM platform_control_plane
WHERE org_id=?

",

'domains'=>

"

SELECT COUNT(*)
FROM platform_domains
WHERE org_id=?

",

'signals'=>

"

SELECT COUNT(*)
FROM platform_signals
WHERE conference_id=?

"

];

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

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

if(
in_array(
$k,
[
'planes',
'domains'
]
)
){

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

}
else{

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

}

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

}

$out[
'coherence'
]=

min(

100,

65+

(
$out[
'knowledge'
]
/4
)

+

(
$out[
'planes'
]
*5
)

+

(
$out[
'signals'
]
/10
)

);

$out[
'state'
]=

$out[
'coherence'
]>=90

?

'coherent'

:

'forming';

return
$out;

}

}
