<?php

namespace App\Services;

use App\Core\Database;

class PlatformStewardshipService
{

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

$db=
Database::getInstance();

$q=[];

$queries=[

'pillars'=>
"
SELECT COUNT(*)
FROM platform_stewardship
WHERE org_id=?
",

'healthy'=>
"
SELECT COUNT(*)
FROM platform_stewardship
WHERE org_id=?
AND current_health>=target_health
",

'cycles'=>
"
SELECT COUNT(*)
FROM platform_sustainability_cycles
WHERE org_id=?
",

'roadmaps'=>
"
SELECT COUNT(*)
FROM platform_horizon_roadmaps
WHERE org_id=?
"

];

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

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

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

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

}

$q[
'health'
]=

$q[
'pillars'
]

?

min(

100,

round(

(
$q[
'healthy'
]
/
max(
1,
$q[
'pillars'
]
)

)
*
100

)

+

(
$q[
'cycles'
]
*2
)

)

:

0;

$q[
'state'
]=

$q[
'health'
]>=85

?

'sustainable'

:

'developing';

return
$q;

}

}
