<?php
$title = htmlspecialchars($topic->title) . ' - Forum';
$isModerator = isset($_SESSION['user_role']) && in_array($_SESSION['user_role'], ['admin', 'editor']);
?>
<div class="d-sm-flex align-items-center justify-content-between mb-4">
    <h1 class="h3 mb-0 text-white font-weight-bold">
        <?= $topic->is_pinned ? '<i class="fas fa-thumbtack text-warning mr-1"></i>' : '' ?>
        <?= $topic->is_locked ? '<i class="fas fa-lock text-danger mr-1"></i>' : '' ?>
        <?= htmlspecialchars($topic->title) ?>
    </h1>
    <a href="<?= $baseUrl ?>/forum/category/<?= $topic->category_id ?>" class="glass-btn"><i class="fas fa-arrow-left mr-1"></i>Back</a>
</div>

<div class="glass-card mb-4">
    <div class="d-flex">
        <div class="text-center mr-3" style="min-width: 60px;">
            <img src="<?= $baseUrl ?>/img/undraw_profile.svg" class="rounded-circle mb-1" style="width: 40px; height: 40px;">
            <small style="color: rgba(255,255,255,0.5); display: block;"><?= htmlspecialchars($topic->user_name ?? 'Unknown') ?></small>
        </div>
        <div class="flex-grow-1">
            <div style="color: rgba(255,255,255,0.8); line-height: 1.7;"><?= nl2br(htmlspecialchars($topic->body)) ?></div>
            <?php if (!empty($topic->media)): $media = json_decode($topic->media); if ($media): ?>
            <div class="mt-2">
                <?php if (in_array($media->type ?? '', ['jpg','jpeg','png','gif','svg'])): ?>
                <a href="<?= $baseUrl . $media->path ?>" target="_blank"><img src="<?= $baseUrl . $media->path ?>" class="img-fluid rounded" style="max-height: 200px;"></a>
                <?php else: ?>
                <a href="<?= $baseUrl . $media->path ?>" target="_blank" class="btn btn-sm glass-btn"><i class="fas fa-download mr-1"></i><?= htmlspecialchars($media->original_name ?? 'Attachment') ?></a>
                <?php endif; ?>
            </div>
            <?php endif; endif; ?>
            <small style="color: rgba(255,255,255,0.4);">Posted <?= date('M d, Y H:i', strtotime($topic->created_at)) ?> | <?= $topic->views ?> views</small>
            <?php if ($isModerator): ?>
            <div class="mt-2">
                <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/pin" style="display:inline;">
                    <button type="submit" class="btn btn-sm <?= $topic->is_pinned ? 'btn-warning' : 'btn-outline-warning' ?>"><i class="fas fa-thumbtack mr-1"></i><?= $topic->is_pinned ? 'Unpin' : 'Pin' ?></button>
                </form>
                <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/lock" style="display:inline;">
                    <button type="submit" class="btn btn-sm <?= $topic->is_locked ? 'btn-danger' : 'btn-outline-danger' ?>"><i class="fas fa-lock mr-1"></i><?= $topic->is_locked ? 'Unlock' : 'Lock' ?></button>
                </form>
                <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this topic?');">
                    <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash mr-1"></i>Delete</button>
                </form>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php if (!empty($posts)): ?>
    <h5 class="text-white font-weight-bold mb-3">Replies (<?= count($posts) ?>)</h5>
    <?php foreach ($posts as $post): ?>
    <div class="glass-card mb-3">
        <div class="d-flex">
            <div class="text-center mr-3" style="min-width: 60px;">
                <img src="<?= $baseUrl ?>/img/undraw_profile.svg" class="rounded-circle mb-1" style="width: 40px; height: 40px;">
                <small style="color: rgba(255,255,255,0.5); display: block;"><?= htmlspecialchars($post->user_name ?? 'Unknown') ?></small>
            </div>
            <div class="flex-grow-1">
                <div style="color: rgba(255,255,255,0.8); line-height: 1.7;"><?= nl2br(htmlspecialchars($post->body)) ?></div>
                <?php if (!empty($post->media)): $pm = json_decode($post->media); if ($pm): ?>
                <div class="mt-2">
                    <?php if (in_array($pm->type ?? '', ['jpg','jpeg','png','gif','svg'])): ?>
                    <a href="<?= $baseUrl . $pm->path ?>" target="_blank"><img src="<?= $baseUrl . $pm->path ?>" class="img-fluid rounded" style="max-height: 200px;"></a>
                    <?php else: ?>
                    <a href="<?= $baseUrl . $pm->path ?>" target="_blank" class="btn btn-sm glass-btn"><i class="fas fa-download mr-1"></i><?= htmlspecialchars($pm->original_name ?? 'Attachment') ?></a>
                    <?php endif; ?>
                </div>
                <?php endif; endif; ?>
                <small style="color: rgba(255,255,255,0.4);">Posted <?= date('M d, Y H:i', strtotime($post->created_at)) ?></small>
                <?php if ($isModerator): ?>
                <div class="mt-1">
                    <form method="POST" action="<?= $baseUrl ?>/forum/post/<?= $post->id ?>/delete" style="display:inline;" onsubmit="return confirm('Delete this reply?');">
                        <button type="submit" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button>
                    </form>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
<?php endif; ?>

<?php if ($topic->is_locked && !$isModerator): ?>
<div class="glass-card mt-4 text-center" style="opacity: 0.7;">
    <p class="mb-0"><i class="fas fa-lock mr-2"></i>This topic is locked. No new replies can be posted.</p>
</div>
<?php else: ?>
<div class="glass-card mt-4">
    <h5 class="font-weight-bold mb-3">Post a Reply</h5>
    <form method="POST" action="<?= $baseUrl ?>/forum/topic/<?= $topic->id ?>/reply" enctype="multipart/form-data">
        <div class="form-group">
            <textarea name="body" class="form-control glass-input" rows="4" required placeholder="Write your reply..."></textarea>
        </div>
        <div class="form-group">
            <label style="color: rgba(255,255,255,0.7); font-size: 0.9rem;">Attachment (optional)</label>
            <input type="file" name="media" class="form-control-file" style="color: rgba(255,255,255,0.7);">
        </div>
        <button type="submit" class="glass-btn glass-btn-primary">Post Reply</button>
    </form>
</div>
<?php endif; ?>