<?php

namespace App\Services;

use App\Core\Database;

class PlatformHorizonService
{
public static function dashboard(
int $orgId
): array
{
$db=
Database::getInstance();

$out=[];

$queries=[

'roadmaps'=>

"

SELECT COUNT(*)
FROM platform_horizon_roadmaps
WHERE org_id=?

",

'active'=>

"

SELECT COUNT(*)
FROM platform_horizon_roadmaps
WHERE org_id=?
AND state='active'

",

'milestones'=>

"

SELECT COUNT(*)

FROM platform_horizon_milestones hm

INNER JOIN
platform_horizon_roadmaps hr

ON hr.id=hm.roadmap_id

WHERE hr.org_id=?

",

'completed'=>

"

SELECT COUNT(*)

FROM platform_horizon_milestones hm

INNER JOIN
platform_horizon_roadmaps hr

ON hr.id=hm.roadmap_id

WHERE hr.org_id=?

AND hm.progress_percent>=100

"

];

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

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

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

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

}

$out[
'trajectory'
]=

$out[
'milestones'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'active'
]
*4
)

)

:

0;

$out[
'state'
]=

$out[
'trajectory'
]>=80

?

'on_track'

:

'planning';

return
$out;

}

}
