<?php
declare(strict_types=1);

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

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

$sql = "SELECT c.id, c.application_id, c.is_internal, c.created_at, c.comment,
               a.application_number,
               CONCAT(TRIM(u.first_name),' ',TRIM(u.last_name)) AS author_name
        FROM application_comments c
        INNER JOIN applications a ON c.application_id = a.id
        INNER JOIN users u ON c.user_id = u.id
        ORDER BY c.created_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 comments</h1>
        <p>Threaded discussion and internal notes on applications.</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_status_history.php"><i class="ri-git-commit-line"></i> Status history</a>
        </div>
    </div>

    <div class="section-card">
        <div class="section-header">
            <h2><i class="ri-chat-3-line"></i> Comments</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>Author</th>
                        <th>Internal</th>
                        <th>Comment (excerpt)</th>
                        <th>Posted</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($rows)): ?>
                    <tr class="empty-row"><td colspan="5"><i class="ri-inbox-line me-2"></i>No comments</td></tr>
                    <?php else: ?>
                        <?php foreach ($rows as $r): ?>
                        <?php
                            $com = (string) ($r['comment'] ?? '');
                            $snip = strlen($com) > 140 ? substr($com, 0, 137) . '...' : $com;
                        ?>
                        <tr>
                            <td class="iscms-mono" style="font-weight:600; color:var(--ssbs-blue)"><?= iscms_h($r['application_number']) ?></td>
                            <td><small><?= iscms_h(trim((string) ($r['author_name'] ?? ''))) ?></small></td>
                            <td><?= !empty($r['is_internal']) ? '<span class="badge bg-warning-subtle text-dark" style="font-size:.65rem">Internal</span>' : '<span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem">External</span>' ?></td>
                            <td><small><?= iscms_h($snip) ?></small></td>
                            <td><small class="text-muted"><?= !empty($r['created_at']) ? iscms_h(substr((string) $r['created_at'], 0, 19)) : '-' ?></small></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
            <?php endif; ?>
        </div>
    </div>
<?php
iscms_render_footer();
