<?php

namespace App\Services;

use App\Core\Database;

class PlatformKernelService
{

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

$db=
Database::getInstance();

$out=[];

$checks=[

'runtime'=>

"

SELECT COUNT(*)

FROM platform_runtime

WHERE conference_id=?

",

'snapshots'=>

"

SELECT COUNT(*)

FROM platform_state_snapshots

WHERE conference_id=?

",

'events'=>

"

SELECT COUNT(*)

FROM platform_event_bus

WHERE conference_id=?

",

'commands'=>

"

SELECT COUNT(*)

FROM conference_action_registry

WHERE is_enabled=1

"

];

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

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

if(
$k==='commands'
){

$stmt
->execute();

}
else{

$stmt
->execute([

$conferenceId

]);

}

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

}

$out[
'kernel_health'
]=

min(

100,

60+

$out[
'runtime'
]

+

(
$out[
'events'
]/5
)

);

$out[
'runtime_state'
]=

$out[
'kernel_health'
]>=85

?

'stable'

:

'warming';

return
$out;

}

public static function snapshot(
int $conferenceId,
int $userId
): bool
{

$db=
Database::getInstance();

return
$db
->prepare("

INSERT INTO
platform_state_snapshots(

conference_id,

snapshot_type,

snapshot_json,

created_by

)

VALUES(

?,

'manual',

'{}',

?

)

")

->execute([

$conferenceId,

$userId

]);

}

}
