<?php

namespace App\Services;

use App\Core\Database;

class HomepageEnterpriseBrainService
{

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

$db=
Database::getInstance();

$out=[];

$metrics=[

'blocks'=>

"

SELECT COUNT(*)

FROM conference_homepage_blocks

WHERE conference_id=?

",

'deployments'=>

"

SELECT COUNT(*)

FROM conference_home_deployments

WHERE conference_id=?

",

'incidents'=>

"

SELECT COUNT(*)

FROM conference_home_incidents

WHERE conference_id=?

"

];

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

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

$stmt
->execute([

$conferenceId

]);

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

}

$score=

min(

100,

50+

$out[
'blocks'
]

+

(
$out[
'deployments'
]*8
)

-

(
$out[
'incidents'
]*10
)

);

$recommend=[];

if(
$out[
'blocks'
]<12
){

$recommend[]=
'Expand homepage content';

}

if(
$out[
'deployments'
]<2
){

$recommend[]=
'Enable staged publishing';

}

if(
$out[
'incidents'
]>0
){

$recommend[]=
'Resolve active incidents';

}

return [

'brain_score'=>

max(
0,
$score
),

'recommendations'=>

$recommend,

'state'=>

$score>=85

?

'optimized'

:

'improve'

];

}

}
