<?php

namespace App\Services;

use App\Core\Database;

class PlatformMinistryService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'frameworks'=>

"

SELECT COUNT(*)

FROM platform_regulatory_framework

WHERE org_id=?

",

'compliant'=>

"

SELECT COUNT(*)

FROM platform_regulatory_framework

WHERE org_id=?

AND compliance_percent>=80

",

'kpis'=>

"

SELECT COUNT(*)

FROM platform_national_kpis

WHERE org_id=?

",

'observatory'=>

"

SELECT COUNT(*)

FROM platform_observatory_metrics

WHERE org_id=?

"

];

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

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

$stmt
->execute([
$orgId
]);

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

}

$out[
'alignment'
]=

$out[
'frameworks'
]

?

min(

100,

round(

(
$out[
'compliant'
]
/
max(
1,
$out[
'frameworks'
]
)

)
*
100

)

+

(
$out[
'kpis'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'alignment'
]>=80

?

'aligned'

:

'monitoring';

return
$out;

}

}
