<?php
/**
 * IDGForum - Forum Index (Categories & Boards)
 */
require_once dirname(__DIR__) . '/includes/functions.php';

// Verificăm dacă forumul este instalat, dacă nu redirecționăm la installer
if (!file_exists(dirname(__DIR__) . '/data/install.lock')) {
    header('Location: ../installer/index.php');
    exit;
}

$theme = new IDGTheme();
$forum = new IDGForum(idg_getCurrentUser());

$categories = $forum->getCategories(0); // Categorii principale

$theme->renderHeader('Index Forum');
?>

<div class="topics-header">
    <div class="topics-title">
        <h2>Comunitatea Forum</h2>
        <p>Bine ai venit! Alege un board de mai jos pentru a începe discuția.</p>
    </div>
    
    <div class="global-stats-bar" style="background-color: var(--card-bg); border: 1px solid var(--border-color); border-radius: 8px; padding: 0.5rem 1rem; font-size: 0.85rem; display: flex; gap: 1rem; backdrop-filter: blur(5px);">
        <?php $stats = $forum->getStats(); ?>
        <span><strong>Membri:</strong> <?php echo $stats['users']; ?></span> |
        <span><strong>Topicuri:</strong> <?php echo $stats['topics']; ?></span> |
        <span><strong>Postări:</strong> <?php echo $stats['posts']; ?></span>
    </div>
</div>

<div class="forum-categories-wrapper" style="display: flex; flex-direction: column; gap: 2rem;">
    <?php if (empty($categories)): ?>
        <div class="alert alert-info">
            <i class="fa-solid fa-circle-info"></i>
            <div>Nu au fost create categorii de forum încă. Autentifică-te ca administrator pentru a configura forumul.</div>
        </div>
    <?php else: ?>
        <?php foreach ($categories as $cat): ?>
            <div class="category-box">
                <div class="category-header">
                    <h2><?php echo htmlspecialchars($cat['name']); ?></h2>
                    <?php if (!empty($cat['description'])): ?>
                        <p><?php echo htmlspecialchars($cat['description']); ?></p>
                    <?php endif; ?>
                </div>
                
                <div class="boards-list">
                    <?php 
                    $boards = $forum->getBoards($cat['id']);
                    if (empty($boards)): 
                    ?>
                        <div style="padding: 1.5rem; text-align: center; color: var(--text-muted); font-size: 0.9rem;">
                            Niciun board în această categorie.
                        </div>
                    <?php else: ?>
                        <?php foreach ($boards as $board): ?>
                            <div class="board-row">
                                <div class="board-icon">
                                    <i class="fa-regular fa-comments"></i>
                                </div>
                                <div class="board-info">
                                    <a href="<?php echo idg_url('forum/board.php?id=' . $board['id']); ?>" class="board-name">
                                        <?php echo htmlspecialchars($board['name']); ?>
                                    </a>
                                    <span class="board-desc"><?php echo htmlspecialchars($board['description']); ?></span>
                                </div>
                                <div class="board-stats">
                                    <div><span class="num"><?php echo $board['topics_count']; ?></span> <span class="label">topicuri</span></div>
                                    <div style="margin-top: 4px;"><span class="num"><?php echo $board['posts_count']; ?></span> <span class="label">postări</span></div>
                                </div>
                                <div class="board-lastpost">
                                    <?php if ($board['last_post_id']): 
                                        $lastPost = $forum->getPost($board['last_post_id']);
                                        if ($lastPost):
                                            $lastTopic = $forum->getTopic($lastPost['topic_id']);
                                            $lastUser = new IDGUser($lastPost['user_id']);
                                            $styleClass = $lastUser->active_name_style ? 'style-' . $lastUser->active_name_style : '';
                                    ?>
                                            <a href="<?php echo idg_url('forum/topic.php?id=' . $lastPost['topic_id'] . '#post-' . $lastPost['id']); ?>" class="lastpost-title">
                                                <?php echo htmlspecialchars($lastTopic['title'] ?? 'Topic necunoscut'); ?>
                                            </a>
                                            <span>
                                                de 
                                                <a href="<?php echo idg_url('forum/profile.php?id=' . $lastUser->id); ?>" class="lastpost-user <?php echo $styleClass; ?>" data-user-id="<?php echo $lastUser->id; ?>">
                                                    <?php echo htmlspecialchars($lastUser->getDisplayName()); ?>
                                                </a>
                                            </span>
                                            <span class="lastpost-time"><?php echo date('d.m.Y H:i', strtotime($lastPost['created_at'])); ?></span>
                                        <?php else: ?>
                                            <span class="text-muted">Nicio postare</span>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <span class="text-muted">Nicio postare</span>
                                    <?php endif; ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    <?php endif; ?>
</div>

<div class="online-users-box" style="margin-top: 2rem; background-color: var(--card-bg); border: 1px solid var(--border-color); border-radius: 12px; padding: 1.5rem; backdrop-filter: blur(10px);">
    <h3 style="font-size: 1.05rem; border-bottom: 1px solid var(--border-color); padding-bottom: 0.5rem; margin-bottom: 0.75rem; display: flex; align-items: center; gap: 0.5rem; font-weight: 700;">
        <i class="fa-solid fa-users text-success"></i> Utilizatori Online (în ultimele 5 minute)
    </h3>
    <div class="online-list" style="display: flex; flex-wrap: wrap; gap: 0.8rem; font-size: 0.9rem;">
        <?php 
        $onlineUsers = $forum->getOnlineUsers();
        if (empty($onlineUsers)):
            echo '<span class="text-muted">Niciun utilizator înregistrat online în acest moment.</span>';
        else:
            foreach ($onlineUsers as $ou):
                $styleClass = $ou['active_name_style'] ? 'style-' . $ou['active_name_style'] : '';
                $roleClass = 'role-' . $ou['role'];
        ?>
                <a href="<?php echo idg_url('forum/profile.php?id=' . $ou['id']); ?>" class="online-user <?php echo $styleClass; ?>" data-user-id="<?php echo $ou['id']; ?>" style="font-weight: 600;">
                    <?php echo htmlspecialchars($ou['display_name'] ?? $ou['username']); ?>
                </a>
        <?php 
            endforeach;
        endif; 
        ?>
    </div>
</div>

<?php
$theme->renderFooter();
?>
