<?php

namespace App\Services;

use App\Core\Database;

class HomepageGovernanceService
{

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

$db=
Database::getInstance();

$out=[];

$stmt=
$db
->prepare("

SELECT COUNT(*)

FROM
conference_home_policies

WHERE

org_id=?

AND

is_enabled=1

");

$stmt
->execute([

$orgId

]);

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

$score=100;

/*
existing signals
*/

$checks=[

"

SELECT COUNT(*)
FROM conference_home_incidents
WHERE conference_id=?

",

"

SELECT COUNT(*)
FROM conference_home_deployments
WHERE conference_id=?

"

];

foreach(
$checks
as $sql
){

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

$q
->execute([

$conferenceId

]);

$score-=

min(
15,
(int)
$q
->fetchColumn()
);

}

$out[
'compliance'
]=

max(
0,
$score
);

$out[
'state'
]=

$out[
'compliance'
]>=85

?

'compliant'

:

'review';

return
$out;

}

}
