<?php

namespace App\Services;

use App\Core\Database;

class PlatformSovereignService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'entities'=>

"

SELECT COUNT(*)

FROM platform_sovereign_entities

",

'mesh'=>

"

SELECT COUNT(*)

FROM platform_governance_mesh

WHERE org_id=?

",

'frameworks'=>

"

SELECT COUNT(*)

FROM platform_regulatory_framework

WHERE org_id=?

",

'domains'=>

"

SELECT COUNT(*)

FROM platform_domains

WHERE org_id=?

"

];

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

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

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

$stmt->execute();

}
else{

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

}

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

}

$out[
'sovereignty'
]=

min(

100,

60+

(
$out[
'mesh'
]*3
)

+

(
$out[
'frameworks'
]*2
)

+

$out[
'domains'
]

);

$out[
'state'
]=

$out[
'sovereignty'
]>=85

?

'federated'

:

'building';

return
$out;

}

}
