<?php

namespace App\Services;

use App\Core\Database;

class PlatformSenateService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'committees'=>

"

SELECT COUNT(*)
FROM platform_senate
WHERE org_id=?

",

'resolutions'=>

"

SELECT COUNT(*)
FROM platform_academic_resolutions
WHERE org_id=?

",

'approved'=>

"

SELECT COUNT(*)
FROM platform_academic_resolutions
WHERE org_id=?
AND approval_state='approved'

",

'constitution'=>

"

SELECT COUNT(*)
FROM platform_constitution
WHERE org_id=?
AND is_active=1

"

];

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

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

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

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

}

$out[
'alignment'
]=

$out[
'resolutions'
]

?

min(

100,

round(

(
$out[
'approved'
]
/
max(
1,
$out[
'resolutions'
]
)

)
*
100

)

+

$out[
'constitution'
]

)

:

0;

$out[
'state'
]=

$out[
'alignment'
]>=80

?

'aligned'

:

'reviewing';

return
$out;

}

}
