<?php

namespace App\Services;

use App\Core\Database;

class HomepageStateService
{

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

$db=
Database::getInstance();

$s=
$db
->prepare("

SELECT *

FROM
conference_home_sections

WHERE conference_id=?

");

$s
->execute([

$conferenceId

]);

$b=
$db
->prepare("

SELECT *

FROM
conference_homepage_blocks

WHERE conference_id=?

");

$b
->execute([

$conferenceId

]);

$data=[

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

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

];

return
$db
->prepare("

INSERT INTO
conference_home_state(

conference_id,

state_type,

snapshot_json,

created_by

)

VALUES(

?,

'manual',

?,

?

)

")

->execute([

$conferenceId,

json_encode(
$data,
JSON_UNESCAPED_UNICODE
),

$userId

]);

}

public static function latest(
int $conferenceId
)
{

$db=
Database::getInstance();

$stmt=
$db
->prepare("

SELECT *

FROM
conference_home_state

WHERE conference_id=?

ORDER BY id DESC

LIMIT 1

");

$stmt
->execute([

$conferenceId

]);

return
$stmt
->fetch()

??

[];

}

}
