<?php

namespace App\Services;

use App\Core\Database;

class HomepageProgramOfficeService
{

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

$db=
Database::getInstance();

$out=[];

$queries=[

'programs'=>

"

SELECT COUNT(*)

FROM conference_home_programs

WHERE conference_id=?

",

'active'=>

"

SELECT COUNT(*)

FROM conference_home_programs

WHERE conference_id=?

AND program_state='active'

",

'initiatives'=>

"

SELECT COUNT(*)

FROM conference_home_initiatives i

JOIN conference_home_programs p

ON p.id=i.program_id

WHERE p.conference_id=?

",

'completed'=>

"

SELECT COUNT(*)

FROM conference_home_initiatives i

JOIN conference_home_programs p

ON p.id=i.program_id

WHERE

p.conference_id=?

AND

i.completion_percent>=100

"

];

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

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

$stmt
->execute([

$conferenceId

]);

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

}

$out[
'delivery'
]=

$out[
'initiatives'
]

?

round(

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

)
*
100

)

:

0;

$out[
'state'
]=

$out[
'delivery'
]>=70

?

'on_schedule'

:

'attention';

return
$out;

}

}
