<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'video gallery';
$pdo = getDB();

$hasFile = false;
try {
    $pdo->query("SELECT video_file FROM videos LIMIT 1");
    $hasFile = true;
} catch (Exception $e) {}

if ($hasFile) {
    $stmt = $pdo->query("SELECT id, title, video_url, video_file, video_type, description FROM videos WHERE is_active = 1 ORDER BY sort_order, id");
} else {
    $stmt = $pdo->query("SELECT id, title, video_url, description FROM videos WHERE is_active = 1 ORDER BY sort_order, id");
}
$videos = $stmt->fetchAll(PDO::FETCH_ASSOC);

function embedVideoUrl($url) {
    $url = trim($url);
    if (preg_match('/youtube\.com\/embed\/([a-zA-Z0-9_-]+)/', $url, $m)) return 'https://www.youtube.com/embed/' . $m[1];
    if (preg_match('/youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)/', $url, $m)) return 'https://www.youtube.com/embed/' . $m[1];
    if (preg_match('/youtu\.be\/([a-zA-Z0-9_-]+)/', $url, $m)) return 'https://www.youtube.com/embed/' . $m[1];
    return $url;
}

$assetBase = (BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
require_once __DIR__ . '/includes/header.php';
?>
<div class="page-content video-gallery-page">
    <div class="container">
        <h1>Video Gallery</h1>
        <div class="video-grid">
            <?php foreach ($videos as $v):
                $type = isset($v['video_type']) ? $v['video_type'] : 'youtube';
                $isUpload = $type === 'upload' && !empty($v['video_file']);
            ?>
            <div class="video-item">
                <div class="video-wrapper">
                    <?php if ($isUpload):
                        $src = $assetBase . 'uploads/videos/' . rawurlencode($v['video_file']);
                    ?>
                    <?php
                        $ext = strtolower(pathinfo($v['video_file'], PATHINFO_EXTENSION));
                        $mime = ($ext === 'webm') ? 'webm' : (($ext === 'ogg') ? 'ogg' : 'mp4');
                    ?>
                    <video class="video-player" controls playsinline preload="metadata">
                        <source src="<?php echo htmlspecialchars($src); ?>" type="video/<?php echo $mime; ?>">
                        Your browser does not support the video tag.
                    </video>
                    <?php else:
                        $embed = embedVideoUrl($v['video_url'] ?? '');
                        if ($embed): ?>
                    <iframe class="video-iframe" src="<?php echo htmlspecialchars($embed); ?>?rel=0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen title="<?php echo htmlspecialchars($v['title']); ?>"></iframe>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
                <h3><?php echo htmlspecialchars($v['title']); ?></h3>
                <?php if (!empty($v['description'])): ?>
                <p><?php echo nl2br(htmlspecialchars($v['description'])); ?></p>
                <?php endif; ?>
            </div>
            <?php endforeach; ?>
        </div>
        <?php if (empty($videos)): ?>
        <p class="no-items">No videos yet.</p>
        <?php endif; ?>
    </div>
</div>
<?php require_once __DIR__ . '/includes/footer.php'; ?>
