<?php

namespace App\Services;

use App\Core\Database;

class PlatformTrustService
{

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

$db=
Database::getInstance();

$queries=[

'trust'=>
"
SELECT COUNT(*)
FROM platform_trust_registry
WHERE org_id=?
",

'audit'=>
"
SELECT COUNT(*)
FROM platform_audit_fabric
WHERE conference_id=?
",

'evidence'=>
"
SELECT COUNT(*)
FROM platform_evidence_chain
WHERE conference_id=?
",

'verified'=>
"
SELECT COUNT(*)
FROM platform_evidence_chain
WHERE conference_id=?
AND verification_state='verified'
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'audit',
'evidence',
'verified'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'integrity'
]=

min(

100,

60+

(
$out[
'audit'
]
/2
)

+

(
$out[
'verified'
]
*3
)

);

$out[
'state'
]=

$out[
'integrity'
]>=85

?

'trusted'

:

'observing';

return $out;

}

}
