<?php

namespace App\Services;

use App\Core\Database;

class HomepageBranchService
{

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

$db=
Database::getInstance();

$data=[];

$s=
$db
->prepare("

SELECT *

FROM conference_home_sections

WHERE conference_id=?

");

$s
->execute([

$conferenceId

]);

$data[
'sections'
]=
$s
->fetchAll();

$b=
$db
->prepare("

SELECT *

FROM conference_homepage_blocks

WHERE conference_id=?

");

$b
->execute([

$conferenceId

]);

$data[
'blocks'
]=
$b
->fetchAll();

return
$db
->prepare("

INSERT INTO
conference_home_branches(

conference_id,

branch_name,

snapshot_json,

created_by

)

VALUES(

?,

?,

?,

?

)

")

->execute([

$conferenceId,

'Draft '
.
date(
'Ymd_His'
),

json_encode(
$data,
JSON_UNESCAPED_UNICODE
),

$userId

]);

}

public static function latest(
int $conferenceId
)
{

$db=
Database::getInstance();

$stmt=
$db
->prepare("

SELECT *

FROM
conference_home_branches

WHERE conference_id=?

ORDER BY id DESC

LIMIT 1

");

$stmt
->execute([

$conferenceId

]);

return
$stmt
->fetch()

??

[];

}

}
