<?php
session_start();
include __DIR__ . '/includes/header.php';
include __DIR__ . '/includes/sidebar.php';

$newsFile = __DIR__ . '/data/news.txt'; // updated path to match admin
$news = file_exists($newsFile) ? file($newsFile, FILE_IGNORE_NEW_LINES) : [];

/* Convert to array of posts */
$posts = [];
foreach ($news as $line) {
    [$id, $title, $author, $date, $category, $content, $featured, $image] = explode('|', $line);
    $posts[] = [
        'id' => $id,
        'title' => $title,
        'author' => $author,
        'date' => $date,
        'category' => $category,
        'content' => $content,
        'featured' => $featured,
        'image' => $image
    ];
}

/* Featured post first */
usort($posts, function($a, $b) {
    if ($a['featured'] === 'yes' && $b['featured'] !== 'yes') return -1;
    if ($b['featured'] === 'yes' && $a['featured'] !== 'yes') return 1;
    return strtotime($b['date']) - strtotime($a['date']);
});

/* Pagination */
$perPage = 5;
$totalPosts = count($posts);
$totalPages = ceil($totalPosts / $perPage);
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$start = ($page - 1) * $perPage;
$visiblePosts = array_slice($posts, $start, $perPage);
?>

<style>
.news-card {
    padding: 30px;
    margin-bottom: 30px;
    text-align: left;
}

.news-card h2 {
    color: #4ff;
    margin-bottom: 10px;
    text-shadow: 0 0 10px rgba(0,255,255,0.45);
}
</style>

<div class="main-wrap">
    <div class="content-panel">

        <!-- NEON BANNER -->
        <div class="news-banner">
            <h1 class="news-banner-text">🔥 Latest News</h1>
        </div>

        <div class="vinyl-divider center-divider"></div>

        <!-- POSTS -->
        <?php if (!$posts): ?>
            <div class="content-box center-text">
                <h3 class="section-title">No News Yet</h3>
                <p class="about-text">Stay tuned — updates are coming soon.</p>
            </div>
        <?php else: ?>

            <?php foreach ($visiblePosts as $post): ?>
                <div class="neon-card news-card">

                    <h2 class="section-title"><?= htmlspecialchars($post['title']) ?></h2>

                    <p><strong style="color:#4ff;">Author:</strong> <?= htmlspecialchars($post['author']) ?></p>
                    <p><strong style="color:#4ff;">Date:</strong> <?= date('M j, Y g:i A', strtotime($post['date'])) ?></p>
                    <p><strong style="color:#4ff;">Category:</strong> <?= htmlspecialchars($post['category']) ?></p>

                    <?php if (!empty($post['image'])): ?>
                        <img src="uploads/news/<?= htmlspecialchars($post['image']) ?>"
                             style="max-width:200px;border:2px solid #4ff;border-radius:8px;margin-bottom:10px;">
                    <?php endif; ?>

                    <p class="about-text"><?= nl2br(htmlspecialchars($post['content'])) ?></p>

                    <?php if ($post['featured'] === 'yes'): ?>
                        <p style="color:#ff0033;font-weight:bold;">⭐ Featured Post</p>
                    <?php endif; ?>

                </div>

                <div class="vinyl-divider center-divider"></div>
            <?php endforeach; ?>

        <?php endif; ?>

        <!-- PAGINATION -->
        <div class="pagination center-text">
            <?php if ($page > 1): ?>
                <a class="button radio-static" href="?page=<?= $page - 1 ?>">⬅ Previous</a>
            <?php endif; ?>

            <?php if ($page < $totalPages): ?>
                <a class="button radio-static" href="?page=<?= $page + 1 ?>">Next ➡</a>
            <?php endif; ?>
        </div>

        <div class="vinyl-divider center-divider"></div>

        <!-- BACK BUTTON -->
        <div class="content-box center-text">
            <a href="index.php" class="button radio-static">⬅ Back to Home</a>
        </div>

        <?php include __DIR__ . '/includes/footer.php'; ?>
    </div>
</div>
