<?php

declare(strict_types=1);

require_once __DIR__ . '/helpers.php';

media_module_boot();

$pageTitle = 'Media';
$newsPosts = news_list();
$galleryItems = media_gallery_list();

require dirname(__DIR__, 2) . '/includes/header.php';
require dirname(__DIR__, 2) . '/includes/sidebar.php';
require dirname(__DIR__, 2) . '/includes/topbar.php';
require dirname(__DIR__, 2) . '/includes/flash.php';
?>
<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <h2 class="h4 mb-0">Media and publishing</h2>
        </div>
        <div class="d-flex gap-2">
            <a class="btn btn-outline-jlfc" href="<?php echo e(base_url('app/modules/media/create-gallery.php')); ?>"><i class="bi bi-images"></i> Upload media</a>
            <a class="btn btn-jlfc" href="<?php echo e(base_url('app/modules/media/create-news.php')); ?>"><i class="bi bi-megaphone"></i> Add news</a>
        </div>
    </div>
    <div class="row g-3">
        <div class="col-md-4">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-newspaper"></i></div>
                <span class="metric-label">News posts</span>
                <strong class="metric-value"><?php echo e((string) count($newsPosts)); ?></strong>
                <small class="text-muted">Draft and published</small>
            </article>
        </div>
        <div class="col-md-4">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-broadcast"></i></div>
                <span class="metric-label">Published news</span>
                <strong class="metric-value"><?php echo e((string) count(array_filter($newsPosts, static fn(array $post): bool => ($post['status'] ?? '') === 'published'))); ?></strong>
                <small class="text-muted">Visible on public site</small>
            </article>
        </div>
        <div class="col-md-4">
            <article class="metric-card h-100">
                <div class="metric-icon"><i class="bi bi-images"></i></div>
                <span class="metric-label">Gallery items</span>
                <strong class="metric-value"><?php echo e((string) count($galleryItems)); ?></strong>
                <small class="text-muted">Image and video uploads</small>
            </article>
        </div>
    </div>
</section>

<section class="panel-card mb-4">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Newsroom</p>
            <h3 class="h5 mb-0">News posts</h3>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Status</th>
                    <th>Author</th>
                    <th>Published</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($newsPosts === []): ?>
                    <tr>
                        <td colspan="6" class="text-center text-muted py-4">No news posts have been added yet.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($newsPosts as $post): ?>
                    <tr>
                        <td>
                            <strong><?php echo e($post['title']); ?></strong>
                            <div class="small text-muted">/<?php echo e($post['slug']); ?></div>
                        </td>
                        <td><span class="badge <?php echo e(news_category_badge_class((string) ($post['category'] ?? 'general'))); ?>"><?php echo e(news_category_label((string) ($post['category'] ?? 'general'))); ?></span></td>
                        <td><span class="badge <?php echo e(($post['status'] ?? '') === 'published' ? 'badge-soft-success' : 'text-bg-light'); ?>"><?php echo e(ucfirst((string) $post['status'])); ?></span></td>
                        <td><?php echo e($post['author_name'] ?: 'System'); ?></td>
                        <td><?php echo e((string) ($post['published_at'] ?: 'Not published')); ?></td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/media/view-news.php?id=' . (int) $post['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/media/edit-news.php?id=' . (int) $post['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/media/delete-news.php?id=' . (int) $post['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="panel-card">
    <div class="panel-card-header mb-3">
        <div>
            <p class="eyebrow mb-1">Gallery</p>
            <h3 class="h5 mb-0">Uploaded media</h3>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th>Title</th>
                    <th>Type</th>
                    <th>Fixture</th>
                    <th>Club</th>
                    <th class="text-end">Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($galleryItems === []): ?>
                    <tr>
                        <td colspan="5" class="text-center text-muted py-4">No gallery items have been uploaded yet.</td>
                    </tr>
                <?php endif; ?>
                <?php foreach ($galleryItems as $item): ?>
                    <tr>
                        <td>
                            <strong><?php echo e($item['title']); ?></strong>
                            <div class="small text-muted"><?php echo e(date('d M Y', strtotime((string) $item['created_at']))); ?></div>
                        </td>
                        <td><?php echo e(ucfirst((string) $item['media_type'])); ?></td>
                        <td><?php echo e($item['fixture_id'] ? (($item['home_club_name'] ?? '') . ' vs ' . ($item['away_club_name'] ?? '')) : 'None'); ?></td>
                        <td><?php echo e($item['club_name'] ?: 'None'); ?></td>
                        <td class="text-end">
                            <div class="btn-group btn-group-sm">
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/media/view-gallery.php?id=' . (int) $item['id'])); ?>">View</a>
                                <a class="btn btn-outline-secondary" href="<?php echo e(base_url('app/modules/media/edit-gallery.php?id=' . (int) $item['id'])); ?>">Edit</a>
                                <a class="btn btn-outline-danger" href="<?php echo e(base_url('app/modules/media/delete-gallery.php?id=' . (int) $item['id'])); ?>">Delete</a>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>
<?php require dirname(__DIR__, 2) . '/includes/footer.php'; ?>