<?php
declare(strict_types=1);

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

$conn = iscms_init();
$pageTitle = 'Application status history';
$activeNav = 'applications';

$sql = "SELECT h.id, h.application_id, h.previous_status, h.new_status, h.reason, h.notes, h.changed_at,
               a.application_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS changed_by_name
        FROM application_status_history h
        INNER JOIN applications a ON h.application_id = a.id
        INNER JOIN users u ON h.changed_by = u.id
        ORDER BY h.changed_at DESC
        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>Application status history</h1>
        <p>Audit trail of status transitions on each application.</p>
    </div>

    <div class="section-card mb-4">
        <div class="section-header">
            <h2><i class="ri-links-line"></i> Quick links</h2>
        </div>
        <div class="quick-actions">
            <a class="qa-btn" href="applications.php"><i class="ri-file-list-3-line"></i> Applications</a>
            <a class="qa-btn" href="application_comments.php"><i class="ri-chat-3-line"></i> Comments</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-git-commit-line"></i> History</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>Application</th>
                        <th>From</th>
                        <th>To</th>
                        <th>Changed by</th>
                        <th>Reason (excerpt)</th>
                        <th>When</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="6"><i class="ri-inbox-line me-2"></i>No history rows</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $reason = (string) ($r['reason'] ?? '');
                            $reasonDisp = $reason !== '' ? (strlen($reason) > 90 ? substr($reason, 0, 87) . '...' : $reason) : '';
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['application_number']) ?></td>
                            <td><?= iscms_val($r['previous_status'] ?? null) ?></td>
                            <td><?= iscms_status_badge((string) ($r['new_status'] ?? '')) ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['changed_by_name'] ?? ''))) ?></small></td>
                            <td><small><?= $reasonDisp !== '' ? iscms_h($reasonDisp) : '<span class="text-muted">-</span>' ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['changed_at']) ? iscms_h(substr((string) $r['changed_at'], 0, 19)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
