<?php

namespace App\Services;

use App\Core\Database;

class PlatformNationalObservatoryService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'accreditation'=>

"

SELECT COUNT(*)

FROM platform_accreditation_framework

WHERE org_id=?

",

'compliant'=>

"

SELECT COUNT(*)

FROM platform_accreditation_framework

WHERE org_id=?

AND compliance_percent>=80

",

'metrics'=>

"

SELECT COUNT(*)

FROM platform_observatory_metrics

WHERE org_id=?

",

'contracts'=>

"

SELECT COUNT(*)

FROM platform_performance_contracts

WHERE org_id=?

"

];

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

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

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

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

}

$out[
'readiness'
]=

$out[
'accreditation'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'metrics'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'readiness'
]>=80

?

'accredited'

:

'preparing';

return
$out;

}

}
