<?php

namespace App\Services;

use App\Core\Database;

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

$db=
Database::getInstance();

$q=[];

$map=[

'boards'=>
"
SELECT COUNT(*)
FROM platform_continuity_board
WHERE org_id=?
",

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

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

'transfer'=>
"
SELECT COUNT(*)
FROM platform_knowledge_transfer
WHERE conference_id=?
"

];

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

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

$s->execute([
$k==='transfer'
?
$conferenceId
:
$orgId
]);

$q[$k]=(int)
$s->fetchColumn();

}

$q[
'continuity'
]=

$q[
'plans'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$q[
'transfer'
]
*2
)

)

:

0;

$q[
'state'
]=

$q[
'continuity'
]>=80

?

'resilient'

:

'preparing';

return $q;

}
}
