<?php
declare(strict_types=1);

/** @var list<array<string,mixed>> $runs */
/** @var list<array<string,mixed>> $items */
/** @var list<array<string,mixed>> $jobs */
?>
        <div class="col-12">
            <div class="dashboard-card section-card" style="background: rgba(255,255,255,0.70);">
                <div class="section-title mb-2"><?= seoscope_h(__('settings.diagnostics.sync_logs_title')) ?></div>
                <?php if (empty($runs)): ?>
                    <div class="text-muted small"><?= seoscope_h(__('settings.diagnostics.no_runs')) ?></div>
                <?php else: ?>
                    <div class="table-wrap">
                        <div class="table-responsive">
                            <table class="crm-table">
                                <thead>
                                    <tr>
                                        <th><?= seoscope_h(__('settings.diagnostics.run_started')) ?></th>
                                        <th><?= seoscope_h(__('settings.diagnostics.run_status')) ?></th>
                                        <th><?= seoscope_h(__('settings.diagnostics.run_mode')) ?></th>
                                        <th><?= seoscope_h(__('settings.diagnostics.run_range')) ?></th>
                                        <th><?= seoscope_h(__('settings.diagnostics.run_summary')) ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($runs as $r): ?>
                                        <?php if (!is_array($r)) {
                                            continue;
                                        } ?>
                                        <tr>
                                            <td class="text-muted small"><?= seoscope_h((string) ($r['started_at'] ?? '')) ?></td>
                                            <td><?= seoscope_h((string) ($r['status'] ?? '')) ?></td>
                                            <td><?= seoscope_h((string) ($r['mode'] ?? '')) ?></td>
                                            <td class="text-muted small"><?= seoscope_h((string) ($r['range_start'] ?? '')) ?> → <?= seoscope_h((string) ($r['range_end'] ?? '')) ?></td>
                                            <td class="text-muted small">
                                                props <?= seoscope_h((string) ($r['properties_total'] ?? '')) ?> / failed <?= seoscope_h((string) ($r['properties_failed'] ?? '')) ?> ·
                                                days <?= seoscope_h((string) ($r['days_processed'] ?? '')) ?>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if (!empty($items)): ?>
                    <div class="section-title mt-4 mb-2"><?= seoscope_h(__('settings.diagnostics.sync_items_title')) ?></div>
                    <div class="table-wrap">
                        <div class="table-responsive">
                            <table class="crm-table">
                                <thead><tr><th><?= seoscope_h(__('settings.diagnostics.item_site')) ?></th><th><?= seoscope_h(__('settings.diagnostics.item_range')) ?></th><th><?= seoscope_h(__('settings.diagnostics.item_status')) ?></th><th><?= seoscope_h(__('settings.diagnostics.item_error')) ?></th></tr></thead>
                                <tbody>
                                    <?php foreach ($items as $it): ?>
                                        <?php if (!is_array($it)) {
                                            continue;
                                        } ?>
                                        <tr>
                                            <td><code><?= seoscope_h((string) ($it['site_url'] ?? '')) ?></code></td>
                                            <td class="text-muted small"><?= seoscope_h((string) ($it['from_date'] ?? '')) ?> → <?= seoscope_h((string) ($it['to_date'] ?? '')) ?></td>
                                            <td><?= seoscope_h((string) ($it['status'] ?? '')) ?></td>
                                            <td class="text-muted small"><?= seoscope_h((string) ($it['error_summary'] ?? '')) ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endif; ?>

                <?php if (!empty($jobs)): ?>
                    <div class="section-title mt-4 mb-2"><?= seoscope_h(__('projects.queue_recent')) ?></div>
                    <div class="table-wrap">
                        <div class="table-responsive">
                            <table class="crm-table">
                                <thead>
                                    <tr>
                                        <th><?= seoscope_h(__('projects.queue_job')) ?></th>
                                        <th><?= seoscope_h(__('projects.queue_property')) ?></th>
                                        <th><?= seoscope_h(__('projects.queue_status')) ?></th>
                                        <th><?= seoscope_h(__('projects.queue_next')) ?></th>
                                        <th><?= seoscope_h(__('projects.queue_error')) ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($jobs as $j): ?>
                                        <?php if (!is_array($j)) {
                                            continue;
                                        } ?>
                                        <tr>
                                            <td class="text-muted small"><?= seoscope_h((string) ($j['job_type'] ?? '')) ?> <div class="text-muted small"><?= seoscope_h((string) ($j['start_date'] ?? '')) ?> → <?= seoscope_h((string) ($j['end_date'] ?? '')) ?></div></td>
                                            <td class="text-muted small">
                                                <?php if (!empty($j['site_url'])): ?>
                                                    <code><?= seoscope_h((string) $j['site_url']) ?></code>
                                                <?php else: ?>
                                                    #<?= (int) ($j['property_id'] ?? 0) ?>
                                                <?php endif; ?>
                                            </td>
                                            <td><?= seoscope_h((string) ($j['status'] ?? '')) ?></td>
                                            <td class="text-muted small"><?= seoscope_h((string) ($j['next_run_at'] ?? '')) ?></td>
                                            <td class="text-muted small"><?= seoscope_h((string) ($j['last_error'] ?? '')) ?></td>
                                        </tr>
                                    <?php endforeach; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
