<?php

namespace App\Services;

use App\Core\Database;

class HomepageIntelligenceService
{

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

$db=
Database::getInstance();

$metrics=[];

/*
sections
*/

$q=
$db
->prepare("

SELECT COUNT(*)

FROM conference_home_sections

WHERE conference_id=?

");

$q
->execute([

$conferenceId

]);

$metrics[
'sections'
]=
(int)
$q
->fetchColumn();

/*
blocks
*/

$q=
$db
->prepare("

SELECT COUNT(*)

FROM conference_homepage_blocks

WHERE conference_id=?

");

$q
->execute([

$conferenceId

]);

$metrics[
'blocks'
]=
(int)
$q
->fetchColumn();

/*
releases
*/

$q=
$db
->prepare("

SELECT COUNT(*)

FROM conference_home_releases

WHERE conference_id=?

");

$q
->execute([

$conferenceId

]);

$metrics[
'releases'
]=
(int)
$q
->fetchColumn();

$score=
min(
100,

40+

($metrics[
'sections'
]*2)

+

($metrics[
'blocks'
])

+

($metrics[
'releases'
]*3)

);

$decision=

$score>=90

?

'Ready for production'

:

(

$score>=70

?

'Optimize homepage'

:

'Expand homepage content'

);

return [

'score'=>
$score,

'decision'=>
$decision,

'metrics'=>
$metrics

];

}

}
