<?php
declare(strict_types=1);

/** @var array{ok:?string,err:?string} $flash */
/** @var array<string,mixed> $diag */

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

$env = is_array($diag['environment'] ?? null) ? $diag['environment'] : [];
$ext = is_array($env['extensions'] ?? null) ? $env['extensions'] : [];
$runs = is_array($diag['sync_runs_recent'] ?? null) ? $diag['sync_runs_recent'] : [];
$items = is_array($diag['sync_items_recent'] ?? null) ? $diag['sync_items_recent'] : [];
$jobs = is_array($diag['sync_jobs_recent'] ?? null) ? $diag['sync_jobs_recent'] : [];
$gStatus = is_array($diag['google_status'] ?? null) ? $diag['google_status'] : [];
$gDiag = is_array($diag['google_diagnostics'] ?? null) ? $diag['google_diagnostics'] : [];
$checks = is_array($gDiag['checks'] ?? null) ? $gDiag['checks'] : [];
$safeSettings = is_array($diag['settings'] ?? null) ? $diag['settings'] : [];
$breakdownsPolicy = (string) ($safeSettings['gsc.sync.breakdowns_policy'] ?? 'last_28_only');

ob_start();
?>
<div class="dashboard-card section-card">
    <div class="d-flex align-items-center justify-content-between flex-wrap gap-3">
        <div>
            <div class="section-title mb-1"><?= seoscope_h(__('settings.diagnostics.title')) ?></div>
            <div class="section-subtitle mb-0"><?= seoscope_h(__('settings.diagnostics.subtitle')) ?></div>
        </div>
        <form method="post" action="<?= seoscope_h(seoscope_url('/settings/diagnostics/export')) ?>" class="m-0">
            <input type="hidden" name="_csrf" value="<?= seoscope_h($csrf) ?>">
            <button type="submit" class="btn btn-upgrade"><?= seoscope_h(__('settings.diagnostics.export')) ?></button>
        </form>
    </div>

    <div class="row g-3 mt-2">
        <div class="col-12 col-xl-6">
            <div class="dashboard-card section-card" style="background: rgba(255,255,255,0.70);">
                <div class="section-title mb-2"><?= seoscope_h(__('settings.diagnostics.env_title')) ?></div>
                <div class="small text-muted mb-2"><?= seoscope_h(__('settings.diagnostics.generated_at')) ?> <code><?= seoscope_h((string) ($diag['generated_at'] ?? '')) ?></code></div>
                <div class="small text-muted mb-2"><?= seoscope_h(__('settings.diagnostics.php_version')) ?> <code><?= seoscope_h((string) ($env['php_version'] ?? '')) ?></code></div>
                <div class="table-wrap">
                    <div class="table-responsive">
                        <table class="crm-table">
                            <thead><tr><th><?= seoscope_h(__('settings.diagnostics.check')) ?></th><th><?= seoscope_h(__('settings.diagnostics.status')) ?></th></tr></thead>
                            <tbody>
                                <?php foreach (['openssl','curl','zip','pdo_mysql'] as $k): ?>
                                    <?php $ok = !empty($ext[$k]); ?>
                                    <tr>
                                        <td><?= seoscope_h(strtoupper($k)) ?></td>
                                        <td><span class="badge <?= $ok ? 'bg-success' : 'bg-danger' ?>"><?= $ok ? 'OK' : 'FAIL' ?></span></td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-12 col-xl-6">
            <div class="dashboard-card section-card" style="background: rgba(255,255,255,0.70);">
                <div class="section-title mb-2"><?= seoscope_h(__('settings.diagnostics.google_title')) ?></div>
                <div class="small text-muted mb-2"><?= seoscope_h(__('settings.diagnostics.google_connected')) ?> <code><?= seoscope_h((string) ($gStatus['connected_at'] ?? '—')) ?></code></div>
                <div class="small text-muted mb-2"><?= seoscope_h(__('settings.diagnostics.google_reconnect')) ?> <code><?= seoscope_h((string) ($gStatus['reconnect_required'] ?? '0')) ?></code></div>
                <div class="small text-muted mb-2"><?= seoscope_h(__('settings.diagnostics.breakdowns_policy')) ?> <code><?= seoscope_h($breakdownsPolicy) ?></code></div>
                <?php if (!empty($gStatus['token_last_error'])): ?>
                    <div class="small text-muted mb-2"><?= seoscope_h(__('settings.diagnostics.google_last_error')) ?> <code><?= seoscope_h((string) $gStatus['token_last_error']) ?></code></div>
                <?php endif; ?>

                <?php if ($checks !== []): ?>
                    <div class="table-wrap mt-3">
                        <div class="table-responsive">
                            <table class="crm-table">
                                <thead><tr><th><?= seoscope_h(__('settings.diagnostics.check')) ?></th><th><?= seoscope_h(__('settings.diagnostics.status')) ?></th></tr></thead>
                                <tbody>
                                    <?php foreach ($checks as $c): ?>
                                        <?php if (!is_array($c)) continue; ?>
                                        <?php $ok = !empty($c['ok']); ?>
                                        <tr>
                                            <td><?= seoscope_h((string) ($c['label'] ?? '')) ?><div class="text-muted small"><?= seoscope_h((string) ($c['hint'] ?? '')) ?></div></td>
                                            <td><span class="badge <?= $ok ? 'bg-success' : 'bg-danger' ?>"><?= $ok ? 'OK' : 'FAIL' ?></span></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>

        <?php require __DIR__ . '/partials/settings_diagnostics_sync_block.php'; ?>
    </div>
</div>
<?php
$content = (string) ob_get_clean();
require __DIR__ . '/layout_admin.php';

