<?php
require_once __DIR__ . '/admin/includes/data_providers.php';
require_once __DIR__ . '/admin/includes/layout.php';

$pdo = admin_pdo();

$overview = admin_fetch_content_overview($pdo);
$videoStats = $overview['videos'];
$recentUploads = admin_fetch_recent_uploads($pdo, 15);
$pendingVideos = admin_fetch_pending_videos($pdo, 15);
$processingQueue = admin_fetch_processing_queue($pdo, 15);
$liveStreams = admin_fetch_live_stream_list($pdo, 12);

admin_page_start('Content Operations Hub', 'content');
?>

<section class="stats-grid">
    <article class="stat-card">
        <div class="stat-card__icon">&#127916;</div>
        <div class="stat-card__label">Video Library</div>
        <div class="stat-card__value"><?= admin_format_number($videoStats['total']) ?></div>
        <div class="stat-card__meta">
            <span><?= admin_format_number($videoStats['approved']) ?> approved</span>
            <span><?= admin_format_number($videoStats['pending']) ?> pending</span>
        </div>
    </article>
    <article class="stat-card">
        <div class="stat-card__icon">&#128225;</div>
        <div class="stat-card__label">Live Streams</div>
        <div class="stat-card__value"><?= admin_format_number($overview['live']['total']) ?></div>
        <div class="stat-card__meta">
            <span><?= admin_format_number($overview['live']['active']) ?> active</span>
            <span><?= admin_format_number($overview['live']['today']) ?> today</span>
        </div>
    </article>
    <article class="stat-card">
        <div class="stat-card__icon">&#10024;</div>
        <div class="stat-card__label">Shorts &amp; Media</div>
        <div class="stat-card__value"><?= admin_format_number($overview['shorts'] + $overview['images'] + $overview['audio']) ?></div>
        <div class="stat-card__meta">
            <span><?= admin_format_number($overview['shorts']) ?> shorts</span>
            <span><?= admin_format_number($overview['images']) ?> images</span>
        </div>
    </article>
    <article class="stat-card">
        <div class="stat-card__icon">&#128218;</div>
        <div class="stat-card__label">Documents &amp; Blogs</div>
        <div class="stat-card__value"><?= admin_format_number($overview['documents'] + $overview['blogs']) ?></div>
        <div class="stat-card__meta">
            <span><?= admin_format_number($overview['documents']) ?> documents</span>
            <span><?= admin_format_number($overview['blogs']) ?> blogs</span>
        </div>
    </article>
</section>

<div class="grid grid--two">
    <section class="card">
        <div class="card__header">
            <h2>Pending Approvals</h2>
            <a class="admin-button admin-button--ghost" href="/f_modules/m_backend/files.php?s=pending">Moderation queue</a>
        </div>
        <?php if (empty($pendingVideos)): ?>
            <div class="empty-state">No videos waiting for review.</div>
        <?php else: ?>
            <table>
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>User</th>
                        <th>Uploaded</th>
                        <th>Status</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($pendingVideos as $video): ?>
                        <tr>
                            <td><?= admin_escape($video['file_title']) ?></td>
                            <td>#<?= admin_escape((string) $video['usr_id']) ?></td>
                            <td><?= admin_format_datetime($video['upload_date']) ?></td>
                            <td><span class="badge badge--warning"><?= admin_escape(ucfirst($video['processing_status'] ?? 'pending')) ?></span></td>
                            <td><a class="admin-button admin-button--ghost" href="/f_modules/m_backend/files.php?search=<?= urlencode($video['file_key']) ?>">Review</a></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </section>

    <section class="card">
        <div class="card__header">
            <h2>Transcoding Queue</h2>
            <a class="admin-button admin-button--ghost" href="/f_modules/m_backend/queue_management.php">Queue dashboard</a>
        </div>
        <?php if (empty($processingQueue)): ?>
            <div class="empty-state">No videos are currently processing.</div>
        <?php else: ?>
            <table>
                <thead>
                    <tr>
                        <th>Title</th>
                        <th>User</th>
                        <th>Status</th>
                        <th>Updated</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($processingQueue as $video): ?>
                        <tr>
                            <td><?= admin_escape($video['file_title']) ?></td>
                            <td>#<?= admin_escape((string) $video['usr_id']) ?></td>
                            <td><span class="badge badge--warning"><?= admin_escape(ucfirst($video['processing_status'])) ?></span></td>
                            <td><?= admin_escape($video['updated_at'] ?? '') ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php endif; ?>
    </section>
</div>

<section class="card">
    <div class="card__header">
        <h2>Recently Uploaded Videos</h2>
    </div>
    <?php if (empty($recentUploads)): ?>
        <div class="empty-state">No recent uploads yet.</div>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Title</th>
                    <th>User</th>
                    <th>Uploaded</th>
                    <th>Views</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentUploads as $video): ?>
                    <tr>
                        <td><?= admin_escape($video['file_title']) ?></td>
                        <td>#<?= admin_escape((string) $video['usr_id']) ?></td>
                        <td><?= admin_format_datetime($video['upload_date']) ?></td>
                        <td><?= admin_format_number($video['file_views']) ?></td>
                        <td>
                            <span class="badge <?= $video['approved'] === '1' ? 'badge--success' : 'badge--warning' ?>">
                                <?= $video['approved'] === '1' ? 'Approved' : 'Pending' ?>
                            </span>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</section>

<section class="card">
    <div class="card__header">
        <h2>Live Stream Overview</h2>
        <a class="admin-button admin-button--ghost" href="/f_modules/m_backend/branding_management.php">Branding settings</a>
    </div>
    <?php if (empty($liveStreams)): ?>
        <div class="empty-state">No live streams detected.</div>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Stream</th>
                    <th>User</th>
                    <th>Status</th>
                    <th>Viewers</th>
                    <th>Updated</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($liveStreams as $stream): ?>
                    <?php $isLive = ($stream['live_status'] ?? '') === 'live'; ?>
                    <tr>
                        <td><?= admin_escape($stream['stream_title'] ?? $stream['stream_key']) ?></td>
                        <td>#<?= admin_escape((string) $stream['usr_id']) ?></td>
                        <td>
                            <span class="badge <?= $isLive ? 'badge--success' : 'badge--warning' ?>">
                                <?= $isLive ? 'Live' : ucfirst($stream['live_status'] ?? 'offline') ?>
                            </span>
                        </td>
                        <td><?= admin_format_number($stream['viewers'] ?? 0) ?></td>
                        <td><?= admin_escape($stream['updated_at'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</section>

<section class="card">
    <div class="card__header">
        <h2>Creator Operations</h2>
    </div>
    <div class="quick-actions">
        <a class="quick-actions__item" href="/f_modules/m_backend/files.php">
            <div class="quick-actions__icon">&#128451;</div>
            <div class="quick-actions__content">
                <div class="quick-actions__title">Full Library</div>
                <div class="quick-actions__desc">Browse, filter, and batch-manage uploaded assets.</div>
            </div>
        </a>
        <a class="quick-actions__item" href="/f_modules/m_backend/token_customization.php">
            <div class="quick-actions__icon">&#128179;</div>
            <div class="quick-actions__content">
                <div class="quick-actions__title">Token Incentives</div>
                <div class="quick-actions__desc">Adjust creator rewards and platform currency packages.</div>
            </div>
        </a>
        <a class="quick-actions__item" href="/f_modules/m_backend/players.php">
            <div class="quick-actions__icon">&#127916;</div>
            <div class="quick-actions__content">
                <div class="quick-actions__title">Player Configuration</div>
                <div class="quick-actions__desc">Tune playback defaults, ads, and branding.</div>
            </div>
        </a>
        <a class="quick-actions__item" href="/f_modules/m_backend/fingerprint_management.php">
            <div class="quick-actions__icon">&#128274;</div>
            <div class="quick-actions__content">
                <div class="quick-actions__title">Content Protection</div>
                <div class="quick-actions__desc">Monitor fingerprints and protect premium assets.</div>
            </div>
        </a>
    </div>
</section>

<?php
admin_page_end();
