<?php

namespace App\Services;

use App\Core\Database;

class PlatformZenithService
{

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

$db=
Database::getInstance();

$queries=[

'zenith'=>
"
SELECT COUNT(*)
FROM platform_zenith
WHERE org_id=?
",

'peak'=>
"
SELECT COUNT(*)
FROM platform_zenith
WHERE org_id=?
AND performance_score>=peak_score
",

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

'completed'=>
"
SELECT COUNT(*)
FROM platform_excellence_cycles
WHERE conference_id=?
AND achievement_score>=80
"

];

$out=[];

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

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

$stmt->execute([

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

]);

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

}

$out[
'excellence'
]=

$out[
'cycles'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'peak'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'excellence'
]>=85

?

'excellent'

:

'advancing';

return $out;

}

}
