<?php
declare(strict_types=1);

/** @var array{ok:?string,err:?string} $flash */
/** @var string $cronToken */
/** @var string $cronUrl */

$pageTitle = __('settings.automation.page_title');
$pageHeading = __('settings.automation.heading');
$activeNav = 'settings';
$hideHero = true;
$csrf = \SeoScope\Auth\AdminCsrf::token();

ob_start();
?>
<div class="dashboard-card section-card">
    <div class="section-title mb-2"><?= seoscope_h(__('settings.automation.title')) ?></div>
    <div class="section-subtitle mb-3"><?= seoscope_h(__('settings.automation.subtitle')) ?></div>

    <div class="dashboard-card section-card mb-3" style="background: rgba(255,255,255,0.70);">
        <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
            <div>
                <div class="section-title mb-1"><?= seoscope_h(__('settings.automation.cron_url')) ?></div>
                <div class="section-subtitle mb-0"><?= seoscope_h(__('settings.automation.cron_url_help')) ?></div>
            </div>
            <form method="post" action="<?= seoscope_h(seoscope_url('/settings/automation/token')) ?>" class="m-0">
                <input type="hidden" name="_csrf" value="<?= seoscope_h($csrf) ?>">
                <button type="submit" class="btn btn-upgrade" onclick="return confirm('<?= seoscope_h(__('settings.automation.regenerate_confirm')) ?>');">
                    <?= seoscope_h(__('settings.automation.regenerate')) ?>
                </button>
            </form>
        </div>
        <div class="d-flex align-items-center gap-2 flex-wrap">
            <code class="user-select-all" style="word-break: break-all;"><?= seoscope_h($cronUrl) ?></code>
        </div>
        <?php
            $cronU = (string) $cronUrl;
            $cronIsAbsolute = $cronU !== '' && preg_match('#^https?://#i', $cronU) === 1;
            $cronCurlExample = $cronIsAbsolute ? seoscope_cron_curl_example_line($cronU) : '';
        ?>
        <?php if (!$cronIsAbsolute && $cronU !== ''): ?>
            <div class="alert alert-warning small mt-2 mb-0"><?= seoscope_h(__('settings.automation.howto_relative_only')) ?></div>
        <?php endif; ?>
        <div class="text-muted small mt-2"><?= seoscope_h(__('settings.automation.note_lock')) ?></div>

        <div class="mt-3 small">
            <div class="fw-semibold mb-2"><?= seoscope_h(__('settings.automation.howto_title')) ?></div>

            <div class="mb-3">
                <div class="fw-semibold text-muted mb-1"><?= seoscope_h(__('settings.automation.section_plesk')) ?></div>
                <ul class="text-muted mb-0 ps-3">
                    <li><?= seoscope_h(__('settings.automation.plesk_step1')) ?></li>
                    <li>
                        <?= seoscope_h(__('settings.automation.plesk_step2')) ?>
                        <code class="user-select-all"><?= seoscope_h($cronUrl) ?></code>
                    </li>
                    <li><?= seoscope_h(__('settings.automation.plesk_step3')) ?></li>
                </ul>
            </div>

            <div class="mb-3">
                <div class="fw-semibold text-muted mb-1"><?= seoscope_h(__('settings.automation.section_cpanel')) ?></div>
                <ul class="text-muted mb-0 ps-3">
                    <li><?= seoscope_h(__('settings.automation.cpanel_step1')) ?></li>
                    <li>
                        <?= seoscope_h(__('settings.automation.cpanel_step2')) ?>
                        <?php if ($cronCurlExample !== ''): ?>
                            <code class="user-select-all d-inline-block" style="word-break: break-all;"><?= seoscope_h($cronCurlExample) ?></code>
                        <?php endif; ?>
                    </li>
                    <li><?= seoscope_h(__('settings.automation.cpanel_step3')) ?></li>
                </ul>
            </div>

            <div class="mb-3">
                <div class="fw-semibold text-muted mb-1"><?= seoscope_h(__('settings.automation.section_other')) ?></div>
                <ul class="text-muted mb-0 ps-3">
                    <li><?= seoscope_h(__('settings.automation.other_step1')) ?></li>
                    <li><?= seoscope_h(__('settings.automation.other_step2')) ?></li>
                    <li><?= seoscope_h(__('settings.automation.other_step3')) ?></li>
                </ul>
            </div>

            <div class="fw-semibold text-muted mt-3 mb-1"><?= seoscope_h(__('settings.automation.howto_common')) ?></div>
            <ul class="text-muted mb-0 ps-3">
                <li><?= seoscope_h(__('settings.automation.howto_freq')) ?></li>
                <li><?= seoscope_h(__('settings.automation.howto_409')) ?></li>
                <li><?= seoscope_h(__('settings.automation.howto_loops')) ?></li>
                <li><?= seoscope_h(__('settings.automation.howto_token')) ?></li>
            </ul>
        </div>
    </div>

    <div class="dashboard-card section-card" style="background: rgba(255,255,255,0.70);">
        <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
            <div>
                <div class="section-title mb-1"><?= seoscope_h(__('settings.automation.token')) ?></div>
                <div class="section-subtitle mb-0"><?= seoscope_h(__('settings.automation.token_help')) ?></div>
            </div>
        </div>
        <div class="mt-3">
            <code><?= seoscope_h($cronToken !== '' ? $cronToken : __('settings.automation.token_missing')) ?></code>
        </div>
    </div>

    <div class="mt-3">
        <a class="btn btn-link" href="<?= seoscope_h(seoscope_url('/settings')) ?>"><?= seoscope_h(__('settings.back')) ?></a>
    </div>
</div>
<?php
$content = (string) ob_get_clean();
require __DIR__ . '/layout_admin.php';

