<?php

namespace App\Services;

use App\Core\Database;

class PlatformBeaconService
{

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

$db=
Database::getInstance();

$queries=[

'opportunities'=>
"
SELECT COUNT(*)
FROM platform_opportunities
WHERE conference_id=?
",

'qualified'=>
"
SELECT COUNT(*)
FROM platform_opportunities
WHERE conference_id=?
AND opportunity_score>=80
",

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

'compass'=>
"
SELECT COUNT(*)
FROM platform_compass
WHERE org_id=?
"

];

$out=[];

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

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

$stmt->execute([

$k==='compass'
?
$orgId
:
$conferenceId

]);

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

}

$out[
'readiness'
]=

$out[
'opportunities'
]

?

min(

100,

round(

(
$out[
'qualified'
]
/
max(
1,
$out[
'opportunities'
]
)

)
*
100

)

+

(
$out[
'signals'
]
*2
)

)

:

0;

$out[
'state'
]=

$out[
'readiness'
]>=85

?

'opportunity_ready'

:

'scanning';

return $out;

}

}
