<?php
declare(strict_types=1);
/** @var array<string,mixed> $setup */
/** @var bool $isConnected */
/** @var list<array<string,mixed>> $properties */
?>

<?php
    $setup = is_array($setup ?? null) ? $setup : [];
    $credsOk = !empty($setup['credentials']['ok']);
    $connected = !empty($setup['google_connection']['connected']);
    $reconnectRequired = !empty($setup['google_connection']['reconnect_required']);
    $tokenError = is_string($setup['google_connection']['token_error'] ?? null) ? (string) $setup['google_connection']['token_error'] : null;
    $propsCount = (int) ($setup['properties']['count'] ?? 0);
    $cronUrl = is_string($setup['cron']['url'] ?? null) ? (string) $setup['cron']['url'] : '';
    $cronLastRunAt = is_string($setup['cron']['last_run_at'] ?? null) ? (string) $setup['cron']['last_run_at'] : null;
    $cronIsStale = !empty($setup['cron']['is_stale']);

    $oauthRedirectUri = seoscope_google_oauth_redirect_uri();

    $step2Ready = $credsOk;
    $step3Ready = $credsOk && $connected && !$reconnectRequired;
    $step4Ready = $step3Ready;

    $badge = static function (string $text, string $cls): string {
        return '<span class="badge ' . seoscope_h($cls) . '">' . seoscope_h($text) . '</span>';
    };
?>

<div class="dashboard-card section-card mb-4" style="background: rgba(255,255,255,0.70);">
    <div class="section-title mb-1"><?= seoscope_h(__('settings.setup.title')) ?></div>
    <div class="section-subtitle mb-3"><?= seoscope_h(__('settings.setup.subtitle')) ?></div>

    <div class="d-flex flex-column gap-3">
        <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
            <div>
                <div class="fw-semibold"><?= seoscope_h(__('settings.setup.step1.title')) ?></div>
                <div class="text-muted small"><?= seoscope_h(__('settings.setup.step1.subtitle')) ?></div>
                <?php if ($oauthRedirectUri !== ''): ?>
                    <div class="text-muted small mt-2"><?= seoscope_h(__('settings.setup.step1.redirect_help')) ?></div>
                    <div class="mt-1"><code class="small user-select-all d-inline-block" style="word-break: break-all;"><?= seoscope_h($oauthRedirectUri) ?></code></div>
                <?php else: ?>
                    <div class="text-muted small mt-2"><?= seoscope_h(__('settings.setup.step1.redirect_missing')) ?></div>
                <?php endif; ?>
            </div>
            <div class="d-flex align-items-center gap-2">
                <?= $credsOk ? $badge(__('settings.setup.status_done'), 'bg-success') : $badge(__('settings.setup.status_missing'), 'bg-secondary') ?>
                <a class="btn btn-sm btn-outline-secondary" href="#oauth-credentials">
                    <?= seoscope_h(__('settings.setup.step1.cta')) ?>
                </a>
            </div>
        </div>

        <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
            <div>
                <div class="fw-semibold"><?= seoscope_h(__('settings.setup.step2.title')) ?></div>
                <div class="text-muted small"><?= seoscope_h(__('settings.setup.step2.subtitle')) ?></div>
                <?php if (!empty($tokenError)): ?>
                    <div class="text-muted small mt-1"><code><?= seoscope_h(mb_substr($tokenError, 0, 160)) ?></code></div>
                <?php endif; ?>
            </div>
            <div class="d-flex align-items-center gap-2">
                <?php if (!$step2Ready): ?>
                    <?= $badge(__('settings.setup.status_locked'), 'bg-light text-dark') ?>
                    <a class="btn btn-sm btn-outline-secondary disabled" href="#oauth-connect" tabindex="-1" aria-disabled="true">
                        <?= seoscope_h(__('settings.setup.step2.cta')) ?>
                    </a>
                <?php else: ?>
                    <?php if ($connected && !$reconnectRequired): ?>
                        <?= $badge(__('settings.setup.status_connected'), 'bg-success') ?>
                    <?php elseif ($connected && $reconnectRequired): ?>
                        <?= $badge(__('settings.setup.status_needs_attention'), 'bg-warning text-dark') ?>
                    <?php else: ?>
                        <?= $badge(__('settings.setup.status_missing'), 'bg-secondary') ?>
                    <?php endif; ?>
                    <a class="btn btn-sm btn-outline-secondary" href="#oauth-connect">
                        <?= seoscope_h(__('settings.setup.step2.cta')) ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
            <div>
                <div class="fw-semibold"><?= seoscope_h(__('settings.setup.step3.title')) ?></div>
                <div class="text-muted small"><?= seoscope_h(__('settings.setup.step3.subtitle')) ?></div>
            </div>
            <div class="d-flex align-items-center gap-2 flex-wrap">
                <?php if (!$step3Ready): ?>
                    <?= $badge(__('settings.setup.status_locked'), 'bg-light text-dark') ?>
                    <a class="btn btn-sm btn-outline-secondary disabled" href="<?= seoscope_h(seoscope_url('/settings?' . http_build_query(['tab' => 'automation']))) ?>" tabindex="-1" aria-disabled="true">
                        <?= seoscope_h(__('settings.setup.step3.cta')) ?>
                    </a>
                <?php else: ?>
                    <a class="btn btn-sm btn-outline-secondary" href="<?= seoscope_h(seoscope_url('/settings?' . http_build_query(['tab' => 'automation']))) ?>">
                        <?= seoscope_h(__('settings.setup.step3.cta')) ?>
                    </a>
                <?php endif; ?>
            </div>
        </div>

        <div class="d-flex align-items-start justify-content-between gap-3 flex-wrap">
            <div>
                <div class="fw-semibold"><?= seoscope_h(__('settings.setup.step4.title')) ?></div>
                <div class="text-muted small">
                    <?= seoscope_h(__('settings.setup.step4.subtitle_prefix')) ?>
                    <a class="text-decoration-none" href="<?= seoscope_h(seoscope_url('/projects')) ?>">
                        <?= seoscope_h(__('settings.setup.step4.websites')) ?>
                    </a>
                    <?= seoscope_h(__('settings.setup.step4.subtitle_suffix')) ?>
                </div>
            </div>
            <div class="d-flex align-items-center gap-2 flex-wrap">
                <?php if (!$step4Ready): ?>
                    <?= $badge(__('settings.setup.status_locked'), 'bg-light text-dark') ?>
                <?php else: ?>
                    <?= $propsCount > 0 ? $badge(__('settings.setup.status_done'), 'bg-success') : $badge(__('settings.setup.status_missing'), 'bg-secondary') ?>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

