<?php

namespace App\Services;

use App\Core\Database;

class PlatformTimeCapsuleService
{

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

$db=
Database::getInstance();

$queries=[

'capsules'=>
"
SELECT COUNT(*)
FROM platform_time_capsules
WHERE conference_id=?
",

'plans'=>
"
SELECT COUNT(*)
FROM platform_restoration_plans
WHERE org_id=?
",

'ready'=>
"
SELECT COUNT(*)
FROM platform_restoration_plans
WHERE org_id=?
AND readiness_score>=80
",

'chronicle'=>
"
SELECT COUNT(*)
FROM platform_chronicle
WHERE conference_id=?
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'capsules',
'chronicle'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'recovery'
]=

$out[
'plans'
]

?

min(

100,

round(

(
$out[
'ready'
]
/
max(
1,
$out[
'plans'
]
)

)
*
100

)

+

(
$out[
'capsules'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'recovery'
]>=85

?

'recoverable'

:

'preparing';

return $out;

}

}
