<?php
// pages/components/calendar_rotate_token.php
require_once BEEZUI_ROOT . '/components/index.php';

use Components\ConfigStore;
use Components\Router;
use Components\Base;
use Components\Button;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    ConfigStore::rotateCalendarFeedToken();

    if (mh_get('confirmed')) {
        // Return success message for confirm-modal
        header('HX-Trigger: calendar-token-rotated');
        ob_start();
        ?>
        <div class="space-y-4">
            <p class="text-sm text-gray-500">
                <?= Base::h(mh_lng('UI_CALENDAR_SUBSCRIBE_ROTATE_SUCCESS', 'The security token has been renewed successfully. All your calendar links have been updated.')); ?>
            </p>

            <template x-teleport="#confirm-modal-footer">
                <div class="w-full">
                    <?= Button::render(mh_lng('UI_OK', 'OK'), [
                            'variant' => 'primary',
                            'fullWidth' => true,
                            'attrs' => [
                                    'command' => 'close',
                                    'commandfor' => 'confirm-modal',
                            ],
                    ]) ?>
                </div>
            </template>
        </div>
        <span hx-swap-oob="innerHTML:#confirm-modal-title"><?= Base::h(mh_lng('UI_SUCCESS', 'Success')) ?></span>
        <?php
        echo ob_get_clean();
        return;
    }
}

// Redirect to or include the popover partial to refresh it
require BEEZUI_ROOT . '/pages/components/calendar_subscribe_popover.php';
