<?php

namespace App\Services;

use App\Core\Database;

class PlatformConstitutionService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'articles'=>

"

SELECT COUNT(*)

FROM platform_constitution

WHERE org_id=?

AND is_active=1

",

'evaluations'=>

"

SELECT COUNT(*)

FROM platform_policy_evaluations

WHERE conference_id=?

",

'strategy'=>

"

SELECT COUNT(*)

FROM platform_strategy_map

WHERE org_id=?

",

'kpis'=>

"

SELECT COUNT(*)

FROM platform_executive_kpis

WHERE org_id=?

"

];

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

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

if(
$k==='evaluations'
){

$stmt->execute([
$conferenceId
]);

}
else{

$stmt->execute([
$orgId
]);

}

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

}

$out[
'governance'
]=

min(

100,

55+

$out[
'articles'
]

+

(
$out[
'evaluations'
]*3
)

+

(
$out[
'strategy'
]
*2
)

);

$out[
'state'
]=

$out[
'governance'
]>=85

?

'constitutional'

:

'governing';

return
$out;

}

}
