<?php

namespace App\Services;

use App\Core\Database;

class PlatformGravityService
{

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

$db=
Database::getInstance();

$queries=[

'gravity'=>
"
SELECT COUNT(*)
FROM platform_gravity
WHERE org_id=?
",

'strong'=>
"
SELECT COUNT(*)
FROM platform_gravity
WHERE org_id=?
AND mass_score>=80
",

'fields'=>
"
SELECT COUNT(*)
FROM platform_priority_field
WHERE conference_id=?
",

'aligned'=>
"
SELECT COUNT(*)
FROM platform_priority_field
WHERE conference_id=?
AND priority_score>=80
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'fields',
'aligned'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'focus'
]=

$out[
'fields'
]

?

min(

100,

round(

(
$out[
'aligned'
]
/
max(
1,
$out[
'fields'
]
)

)
*
100

)

+

(
$out[
'strong'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'focus'
]>=85

?

'focused'

:

'aligning';

return $out;

}

}
