<?php

namespace App\Services;

use App\Core\Database;

class PlatformPulseService
{

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

$db=
Database::getInstance();

$queries=[

'pulses'=>
"
SELECT COUNT(*)
FROM platform_pulse
WHERE org_id=?
",

'healthy'=>
"
SELECT COUNT(*)
FROM platform_pulse
WHERE org_id=?
AND current_score>=target_score
",

'signals'=>
"
SELECT COUNT(*)
FROM platform_vital_signals
WHERE conference_id=?
",

'alerts'=>
"
SELECT COUNT(*)
FROM platform_vital_signals
WHERE conference_id=?
AND signal_value<threshold_value
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'signals',
'alerts'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'health'
]=

$out[
'pulses'
]

?

max(

0,

min(

100,

round(

(
$out[
'healthy'
]
/
max(
1,
$out[
'pulses'
]
)

)
*
100

)

-

(
$out[
'alerts'
]
*2
)

)

)

:

0;

$out[
'state'
]=

$out[
'health'
]>=85

?

'healthy'

:

'monitoring';

return $out;

}

}
