<?php
// pages/components/save_ui_list_layout.php
// Persist per-user list layout selection. Expects POST: list_id, layout

use Components\Base;
use Components\Data\Live\LiveBeezUIConfigStoreProvider;

header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');

$listId = mh_post('list_id', '');
$layout = mh_post('layout', '');
$allowed = ['stack','grid-2','grid-3','grid-4'];

if ($listId !== '' && in_array($layout, $allowed, true)) {
    try {
        $p = new LiveBeezUIConfigStoreProvider();
        $p->setListLayout($listId, $layout);
        http_response_code(204); // No Content
        exit;
    } catch (\Throwable $e) {
        http_response_code(200);
        echo Base::h(json_encode(['ok' => false]), false);
        exit;
    }
}

http_response_code(400);
echo Base::h(json_encode(['ok' => false, 'reason' => 'bad_request']), false);
