<?php

namespace App\Services;

use App\Core\Database;

class PlatformCompassService
{

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

$db=
Database::getInstance();

$queries=[

'compass'=>
"
SELECT COUNT(*)
FROM platform_compass
WHERE org_id=?
",

'aligned'=>
"
SELECT COUNT(*)
FROM platform_compass
WHERE org_id=?
AND current_heading>=target_heading
",

'cycles'=>
"
SELECT COUNT(*)
FROM platform_navigation_cycles
WHERE conference_id=?
",

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

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'cycles'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'direction'
]=

$out[
'compass'
]

?

min(

100,

round(

(
$out[
'aligned'
]
/
max(
1,
$out[
'compass'
]
)

)
*
100

)

+

(
$out[
'cycles'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'direction'
]>=85

?

'on_course'

:

'navigating';

return $out;

}

}
