<?php
declare(strict_types=1);

require_once __DIR__ . '/includes/iscms_layout.php';

$conn = iscms_init();
iscms_require_roles(['SUPER_ADMIN', 'ADMIN']);

$pageTitle = 'System settings (register)';
$activeNav = 'settings';

$sql = "SELECT s.id, s.setting_key, s.setting_value, s.data_type, s.is_public, s.description, s.updated_at,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS updated_by_name
        FROM system_settings s
        LEFT JOIN users u ON s.updated_by = u.id
        ORDER BY s.setting_key ASC
        LIMIT 500";
$result = $conn->query($sql);
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$dbError = $result ? null : $conn->error;

iscms_render_head($pageTitle);
iscms_render_sidebar($activeNav);
iscms_render_topbar($pageTitle);
?>
    <div class="page-header">
        <h1>System settings</h1>
        <p>Key-value configuration rows. Use the editor for changes.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Actions</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="settings.php"><i class="ri-settings-3-line"></i> Open settings editor</a>
            <a class="qa-btn" href="audit_logs.php"><i class="ri-file-code-line"></i> Audit logs</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-list-settings-line"></i> Keys</h2>
            <span class="text-muted" style="font-size:.75rem"><?= count($rows) ?> shown</span>
        </div>
        <div class="table-responsive">
            <?php if ($dbError): ?>
                <div class="alert alert-danger m-3" style="font-size:.85rem">Unable to load data: <?= iscms_h($dbError) ?></div>
            <?php else: ?>
            <table class="ssbs-table">
                <thead>
                    <tr>
                        <th>Key</th>
                        <th>Value (excerpt)</th>
                        <th>Type</th>
                        <th>Public</th>
                        <th>Updated by</th>
                        <th>Updated</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No settings rows</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $val = (string) ($r['setting_value'] ?? '');
                            $valDisp = strlen($val) > 80 ? substr($val, 0, 77) . '...' : $val;
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600"><small><?= iscms_h($r['setting_key']) ?></small></td>
                            <td><small><?= $valDisp !== '' ? iscms_h($valDisp) : '<span class="text-muted">-</span>' ?></small></td>
                            <td><small><?= iscms_h((string) ($r['data_type'] ?? '')) ?></small></td>
                            <td><?= !empty($r['is_public']) ? '<span class="badge bg-info-subtle text-primary" style="font-size:.65rem">Yes</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">No</span>' ?></td>
                            <td><small><?= iscms_val(trim((string) ($r['updated_by_name'] ?? '')) ?: null) ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['updated_at']) ? iscms_h(substr((string) $r['updated_at'], 0, 19)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
