<?php

namespace App\Services;

use App\Core\Database;

class PlatformChancellorOfficeService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'contracts'=>

"

SELECT COUNT(*)

FROM platform_performance_contracts

WHERE org_id=?

",

'completed'=>

"

SELECT COUNT(*)

FROM platform_performance_contracts

WHERE org_id=?

AND current_score>=target_score

",

'cycles'=>

"

SELECT COUNT(*)

FROM platform_execution_cycles

WHERE org_id=?

",

'strategy'=>

"

SELECT COUNT(*)

FROM platform_strategy_map

WHERE org_id=?

"

];

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

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

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

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

}

$out[
'execution'
]=

$out[
'contracts'
]

?

min(

100,

round(

(
$out[
'completed'
]
/
max(
1,
$out[
'contracts'
]
)

)
*
100

)

+

(
$out[
'cycles'
]*2
)

)

:

0;

$out[
'state'
]=

$out[
'execution'
]>=80

?

'executing'

:

'tracking';

return
$out;

}

}
