<?php

namespace App\Services;

use App\Core\Database;

class PlatformCivilizationService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'nodes'=>

"

SELECT COUNT(*)

FROM platform_knowledge_nodes

WHERE org_id=?
OR org_id IS NULL

",

'edges'=>

"

SELECT COUNT(*)

FROM platform_knowledge_edges

",

'entities'=>

"

SELECT COUNT(*)

FROM platform_sovereign_entities

",

'domains'=>

"

SELECT COUNT(*)

FROM platform_domains

WHERE org_id=?

"

];

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

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

if(
in_array(
$k,
[
'edges',
'entities'
]
)
){

$stmt->execute();

}
else{

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

}

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

}

$out[
'civilization'
]=

min(

100,

60+

(
$out[
'nodes'
]
/2
)

+

(
$out[
'edges'
]
/3
)

+

(
$out[
'entities'
]
*2
)

);

$out[
'state'
]=

$out[
'civilization'
]>=85

?

'connected'

:

'growing';

return
$out;

}

}
