<?php

namespace App\Services;

use App\Core\Database;

class PlatformResonanceService
{

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

$db=
Database::getInstance();

$queries=[

'resonance'=>
"
SELECT COUNT(*)
FROM platform_resonance
WHERE org_id=?
",

'active'=>
"
SELECT COUNT(*)
FROM platform_resonance
WHERE org_id=?
AND signal_strength>=80
",

'loops'=>
"
SELECT COUNT(*)
FROM platform_feedback_loops
WHERE conference_id=?
",

'closed'=>
"
SELECT COUNT(*)
FROM platform_feedback_loops
WHERE conference_id=?
AND loop_state='closed'
"

];

$out=[];

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

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

$stmt->execute([

in_array(
$k,
[
'loops',
'closed'
]
)
?
$conferenceId
:
$orgId

]);

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

}

$out[
'learning'
]=

$out[
'loops'
]

?

min(

100,

round(

(
$out[
'closed'
]
/
max(
1,
$out[
'loops'
]
)

)
*
100

)

+

(
$out[
'active'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'learning'
]>=85

?

'learning'

:

'listening';

return $out;

}

}
