<?php

namespace App\Services;

use App\Core\Database;

class PlatformCrownService
{

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

$db=
Database::getInstance();

$queries=[

'mesh'=>
"
SELECT COUNT(*)
FROM platform_sovereignty_mesh
WHERE org_id=?
",

'aligned'=>
"
SELECT COUNT(*)
FROM platform_sovereignty_mesh
WHERE org_id=?
AND authority_score>=alignment_score
",

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

'healthy'=>
"
SELECT COUNT(*)
FROM platform_governance_observatory
WHERE conference_id=?
AND governance_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[
'aligned'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'governance'
]>=85

?

'governed'

:

'aligning';

return $out;

}

}
