<?php

namespace App\Services;

use App\Core\Database;

class HomepageSimulationService
{

public static function create(
int $conferenceId,
int $userId
): bool
{

$db=
Database::getInstance();

$sections=
$db
->prepare("

SELECT *

FROM conference_home_sections

WHERE conference_id=?

");

$sections
->execute([

$conferenceId

]);

$blocks=
$db
->prepare("

SELECT *

FROM conference_homepage_blocks

WHERE conference_id=?

");

$blocks
->execute([

$conferenceId

]);

$data=[

'sections'=>
$sections
->fetchAll(),

'blocks'=>
$blocks
->fetchAll()

];

return
$db
->prepare("

INSERT INTO
conference_home_simulations(

conference_id,

simulation_name,

snapshot_json,

created_by

)

VALUES(

?,

?,

?,

?

)

")

->execute([

$conferenceId,

'Twin '
.
date(
'Ymd_His'
),

json_encode(
$data,
JSON_UNESCAPED_UNICODE
),

$userId

]);

}

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

$db=
Database::getInstance();

$stmt=
$db
->prepare("

SELECT *

FROM
conference_home_simulations

WHERE conference_id=?

ORDER BY id DESC

LIMIT 1

");

$stmt
->execute([

$conferenceId

]);

return
$stmt
->fetch()

??

[];

}

}
