<?php

namespace App\Services;

use App\Core\Database;

class PlatformContinuumService
{

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

$db=
Database::getInstance();

$queries=[

'mesh'=>
"
SELECT COUNT(*)
FROM platform_runtime_mesh
WHERE org_id=?
",

'stable'=>
"
SELECT COUNT(*)
FROM platform_runtime_mesh
WHERE org_id=?
AND mesh_score>=continuity_score
",

'decisions'=>
"
SELECT COUNT(*)
FROM platform_decision_observatory
WHERE conference_id=?
",

'confident'=>
"
SELECT COUNT(*)
FROM platform_decision_observatory
WHERE conference_id=?
AND confidence_score>=80
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'decisions',
'confident'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'continuity'
]=

$out[
'decisions'
]

?

min(

100,

round(

(
$out[
'confident'
]
/
max(
1,
$out[
'decisions'
]
)

)
*
100

)

+

(
$out[
'stable'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'continuity'
]>=85

?

'continuous'

:

'observing';

return $out;

}

}
