<?php
// Prevent direct access
if (count(get_included_files()) === 1) {
    http_response_code(403);
    exit('Forbidden');
}
$page_title = 'Vale Admin — Evidencia';
ob_start();
?>
<div class="dashboard-container">
    <?php $adminNav = 'logs'; require __DIR__ . '/sidebar.php'; ?>
    <main class="main-content">
        <header class="header-bar"><h2>Evidencia del agente</h2></header>
        <div style="flex:1;overflow-y:auto;padding:1.5rem 2rem;">

    <form method="GET" action="/admin/logs" style="display:flex;gap:.8rem;margin-bottom:1.25rem;font-size:.85rem;align-items:end;flex-wrap:wrap;">
        <label>Tipo
            <select name="type" style="display:block;padding:.45rem;border:1px solid #eadfce;border-radius:6px;min-width:160px;">
                <option value="">Todos</option>
                <?php foreach ($types as $ty): ?>
                    <option value="<?php echo htmlspecialchars($ty); ?>" <?php echo (($_GET['type'] ?? '') === $ty) ? 'selected' : ''; ?>>
                        <?php echo htmlspecialchars($ty); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </label>
        <label>Tenant ID
            <input type="number" name="tenant_id" value="<?php echo htmlspecialchars($_GET['tenant_id'] ?? ''); ?>" style="display:block;padding:.45rem;border:1px solid #eadfce;border-radius:6px;width:100px;">
        </label>
        <button type="submit" style="padding:.5rem 1rem;background:#A8331A;color:#FBF6EE;border:none;border-radius:6px;font-weight:600;cursor:pointer;">Filtrar</button>
    </form>

    <table style="width:100%;border-collapse:collapse;background:#fff;border-radius:10px;overflow:hidden;font-size:.8rem;">
        <thead>
            <tr style="background:#221C18;color:#FBF6EE;text-align:left;">
                <th style="padding:.5rem;">#</th>
                <th style="padding:.5rem;">Fecha</th>
                <th style="padding:.5rem;">Tenant</th>
                <th style="padding:.5rem;">Tipo</th>
                <th style="padding:.5rem;">Gemini</th>
                <th style="padding:.5rem;">Tokens</th>
                <th style="padding:.5rem;">Payload</th>
            </tr>
        </thead>
        <tbody>
            <?php if (empty($logs)): ?>
                <tr><td colspan="7" style="padding:1.5rem;text-align:center;color:#7a6f64;">Sin registros.</td></tr>
            <?php else: ?>
                <?php foreach ($logs as $log): ?>
                    <tr style="border-top:1px solid #eadfce;vertical-align:top;">
                        <td style="padding:.5rem;"><?php echo (int) $log['id']; ?></td>
                        <td style="padding:.5rem;white-space:nowrap;"><?php echo htmlspecialchars($log['created_at']); ?></td>
                        <td style="padding:.5rem;"><?php echo htmlspecialchars($log['tenant_name'] ?? ('#' . ($log['tenant_id'] ?? '—'))); ?></td>
                        <td style="padding:.5rem;"><?php echo htmlspecialchars($log['type']); ?></td>
                        <td style="padding:.5rem;text-align:center;"><?php echo $log['gemini_call'] ? '✓' : '—'; ?></td>
                        <td style="padding:.5rem;"><?php echo $log['tokens'] !== null ? (int) $log['tokens'] : '—'; ?></td>
                        <td style="padding:.5rem;max-width:420px;">
                            <?php if (!empty($log['payload'])): ?>
                                <details>
                                    <summary style="cursor:pointer;color:#A8331A;">ver</summary>
                                    <pre style="white-space:pre-wrap;word-break:break-word;background:#FBF6EE;padding:.6rem;border-radius:6px;margin:.4rem 0 0;max-height:280px;overflow:auto;font-size:.72rem;"><?php
                                        $decoded = json_decode($log['payload'], true);
                                        echo htmlspecialchars($decoded !== null ? json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) : $log['payload']);
                                    ?></pre>
                                </details>
                            <?php else: ?>—<?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
    <p style="color:#7a6f64;font-size:.75rem;margin-top:1rem;">Mostrando los últimos 100 registros.</p>
        </div>
    </main>
</div>
<?php
$content = ob_get_clean();
require dirname(__DIR__) . '/layout.php';
