<?php

namespace App\Services;

use App\Core\Database;

class PlatformAuroraService
{

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

$db=
Database::getInstance();

$queries=[

'aurora'=>
"
SELECT COUNT(*)
FROM platform_aurora
WHERE org_id=?
",

'emerging'=>
"
SELECT COUNT(*)
FROM platform_aurora
WHERE org_id=?
AND emergence_score>=80
",

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

'completed'=>
"
SELECT COUNT(*)
FROM platform_transformation_cycles
WHERE conference_id=?
AND transformation_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[
'transformation'
]=

$out[
'cycles'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'emerging'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'transformation'
]>=85

?

'emerging'

:

'adapting';

return $out;

}

}
