<?php

namespace App\Services;

use App\Core\Database;

class HomepageScorecardService
{

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

$db=
Database::getInstance();

$out=[];

$sql=[

'streams'=>

"

SELECT COUNT(*)

FROM conference_home_value_streams

WHERE conference_id=?

",

'outcomes'=>

"

SELECT COUNT(*)

FROM conference_home_outcomes o

JOIN conference_home_value_streams s

ON s.id=o.stream_id

WHERE s.conference_id=?

",

'completed'=>

"

SELECT COUNT(*)

FROM conference_home_outcomes o

JOIN conference_home_value_streams s

ON s.id=o.stream_id

WHERE

s.conference_id=?

AND

o.actual_score>=o.target_score

"

];

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

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

$stmt
->execute([

$conferenceId

]);

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

}

$out[
'value'
]=

$out[
'outcomes'
]

?

round(

(
$out[
'completed'
]
/
max(
1,
$out[
'outcomes'
]
)

)
*
100

)

:

0;

$out[
'grade'
]=

$out[
'value'
]>=80

?

'A'

:

(

$out[
'value'
]>=60

?

'B'

:

'C'

);

return
$out;

}

}
