<?php
$page_title = 'Cron status';
require __DIR__ . '/_guard.php';

$runs = db_all(
    'SELECT * FROM cron_runs ORDER BY started_at DESC LIMIT 100'
);

// Most recent per job
$latest = db_all(
    "SELECT job_name, MAX(started_at) AS last_run,
            SUM(outcome = 'ok') AS ok_count,
            SUM(outcome = 'failed') AS fail_count,
            COUNT(*) AS total
       FROM cron_runs
       GROUP BY job_name"
);
?>

<?php
$settings_section = 'cron';
require __DIR__ . '/_settings_open.php';
?>

<div class="settings-page-head">
    <h1>Cron jobs</h1>
    <p class="crumb">
        Scheduled tasks. See <code>cron/*.php</code>. Each script runs via CLI
        or via HTTP with <code>?secret=CRON_SECRET</code>.
    </p>
</div>

        <?php if ($latest): ?>
            <div class="card mb-3">
                <h2 style="margin-top:0;">Last run per job</h2>
                <table>
                    <tr><th>Job</th><th>Last run</th><th>Total runs</th><th>OK</th><th>Failed</th></tr>
                    <?php foreach ($latest as $row): ?>
                        <tr>
                            <td><code><?= htmlspecialchars($row['job_name']) ?></code></td>
                            <td><?= date('j M Y H:i', strtotime($row['last_run'])) ?></td>
                            <td><?= (int)$row['total'] ?></td>
                            <td><?= (int)$row['ok_count'] ?></td>
                            <td><?= (int)$row['fail_count'] ?></td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            </div>
        <?php else: ?>
            <div class="alert alert-info">
                <strong>No cron runs logged yet.</strong>
                Trigger a run by visiting
                <code>cron/renewal-reminder.php?secret=…</code> or
                <code>cron/overdue.php?secret=…</code>
            </div>
        <?php endif; ?>

        <div class="card">
            <h2 style="margin-top:0;">Recent runs (100 most recent)</h2>
            <?php if (empty($runs)): ?>
                <p class="muted">None yet.</p>
            <?php else: ?>
                <table>
                    <tr>
                        <th>Started</th>
                        <th>Job</th>
                        <th>Duration</th>
                        <th>Outcome</th>
                        <th>Rows</th>
                        <th>Detail</th>
                    </tr>
                    <?php foreach ($runs as $r):
                        $duration = $r['finished_at']
                            ? (strtotime($r['finished_at']) - strtotime($r['started_at'])) . 's'
                            : '&mdash;';
                    ?>
                        <tr>
                            <td><?= date('j M H:i:s', strtotime($r['started_at'])) ?></td>
                            <td><code><?= htmlspecialchars($r['job_name']) ?></code></td>
                            <td><?= $duration ?></td>
                            <td>
                                <?php $cls = $r['outcome']==='ok'?'tag-ok':($r['outcome']==='failed'?'tag-err':'muted'); ?>
                                <span class="tag <?= $cls ?>"><?= htmlspecialchars($r['outcome']) ?></span>
                            </td>
                            <td><?= (int)$r['rows_processed'] ?></td>
                            <td><?= $r['detail'] ? htmlspecialchars($r['detail']) : '' ?></td>
                        </tr>
                    <?php endforeach; ?>
                </table>
            <?php endif; ?>
        </div>

        <div class="card mt-3">
            <h2 style="margin-top:0;">Setup</h2>
            <p>For a real server, add these lines to crontab:</p>
            <pre><code># Daily at 02:00 — find upcoming renewals
0 2 * * *  php /path/to/buylocal/cron/renewal-reminder.php

# Daily at 02:30 — flag overdue invoices
30 2 * * *  php /path/to/buylocal/cron/overdue.php</code></pre>
            <p>On hosting without cron, set up a URL pinger (UptimeRobot, cron-job.org, etc.)
               to hit these URLs once a day with the secret:</p>
            <pre><code><?= htmlspecialchars(SITE_URL) ?>/cron/renewal-reminder.php?secret=…
<?= htmlspecialchars(SITE_URL) ?>/cron/overdue.php?secret=…</code></pre>
        </div>

<?php require __DIR__ . '/_settings_close.php'; ?>

<?php require __DIR__ . '/_footer.php'; ?>