<?php

namespace App\Services;

use App\Core\Database;

class PlatformOriginService
{

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

$db=
Database::getInstance();

$queries=[

'kernel'=>
"
SELECT COUNT(*)
FROM platform_bootstrap_kernel
WHERE org_id=?
",

'stable'=>
"
SELECT COUNT(*)
FROM platform_bootstrap_kernel
WHERE org_id=?
AND bootstrap_score>=lifecycle_score
",

'observatory'=>
"
SELECT COUNT(*)
FROM platform_lifecycle_observatory
WHERE conference_id=?
",

'healthy'=>
"
SELECT COUNT(*)
FROM platform_lifecycle_observatory
WHERE conference_id=?
AND lifecycle_score>=80
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'observatory',
'healthy'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'lifecycle'
]=

$out[
'observatory'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'stable'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'lifecycle'
]>=85

?

'established'

:

'bootstrapping';

return $out;

}

}
