<?php

namespace App\Services;

use App\Core\Database;

class PlatformSummitService
{

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

$db=
Database::getInstance();

$queries=[

'constitution'=>
"
SELECT COUNT(*)
FROM platform_operating_constitution
WHERE org_id=?
",

'compliant'=>
"
SELECT COUNT(*)
FROM platform_operating_constitution
WHERE org_id=?
AND compliance_score>=target_score
",

'observatory'=>
"
SELECT COUNT(*)
FROM platform_policy_observatory
WHERE conference_id=?
",

'healthy'=>
"
SELECT COUNT(*)
FROM platform_policy_observatory
WHERE conference_id=?
AND policy_score>=80
"

];

$out=[];

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

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

$stmt->execute([

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

]);

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

}

$out[
'governance'
]=

$out[
'observatory'
]

?

min(

100,

round(

(
$out[
'healthy'
]
/
max(
1,
$out[
'observatory'
]
)

)
*
100

)

+

(
$out[
'compliant'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'governance'
]>=85

?

'constitutional'

:

'aligning';

return $out;

}

}
