<?php

namespace App\Services;

use App\Core\Database;

class PlatformLighthouseService
{

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

$db=
Database::getInstance();

$queries=[

'lighthouses'=>
"
SELECT COUNT(*)
FROM platform_lighthouse
WHERE org_id=?
",

'clear'=>
"
SELECT COUNT(*)
FROM platform_lighthouse
WHERE org_id=?
AND visibility_score>=80
",

'signals'=>
"
SELECT COUNT(*)
FROM platform_signal_network
WHERE conference_id=?
",

'strong'=>
"
SELECT COUNT(*)
FROM platform_signal_network
WHERE conference_id=?
AND signal_strength>=80
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'signals',
'strong'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'guidance'
]=

$out[
'signals'
]

?

min(

100,

round(

(
$out[
'strong'
]
/
max(
1,
$out[
'signals'
]
)

)
*
100

)

+

(
$out[
'clear'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'guidance'
]>=85

?

'guided'

:

'observing';

return $out;

}

}
