<?php

namespace App\Services;

use App\Core\Database;

class PlatformFrontierService
{

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

$db=
Database::getInstance();

$queries=[

'frontiers'=>
"
SELECT COUNT(*)
FROM platform_frontier
WHERE org_id=?
",

'ready'=>
"
SELECT COUNT(*)
FROM platform_frontier
WHERE org_id=?
AND readiness_score>=80
",

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

'discoveries'=>
"
SELECT COUNT(*)
FROM platform_discovery_cycles
WHERE conference_id=?
AND discovery_score>=80
"

];

$out=[];

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

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

$stmt->execute([

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

]);

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

}

$out[
'exploration'
]=

$out[
'cycles'
]

?

min(

100,

round(

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

)
*
100

)

+

(
$out[
'ready'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'exploration'
]>=85

?

'frontier_ready'

:

'exploring';

return $out;

}

}
