<?php
/**
 * ITSWEBER CMS – Default Theme: Video Section Template
 * Page Builder section template (3.2.0)
 *
 * Variables: $data (array), $section (DB row), $sectionId (string)
 *
 * @author  Benny Weber
 * @copyright ITSWEBER, www.itsweber.de
 */

// Helper: Extract YouTube video ID
if (!function_exists('extractYouTubeId')) {
    function extractYouTubeId(string $url): string {
        if (preg_match('/(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)([\w-]{11})/', $url, $m)) {
            return $m[1];
        }
        return '';
    }
}

$title        = $data['title'] ?? '';
$subtitle     = $data['subtitle'] ?? '';
$video_type   = $data['video_type'] ?? 'youtube';
$video_url    = $data['video_url'] ?? '';
$poster_image = $data['poster_image'] ?? '';
$autoplay     = (int) ($data['autoplay'] ?? 0);
$cta_text     = $data['cta_text'] ?? '';
$cta_url      = $data['cta_url'] ?? '';
$color_scheme = $data['color_scheme'] ?? '';
$sectionTextColor = $data['section_text_color'] ?? '';

// Build section CSS classes
$sectionClasses = 'video-section';
if ($color_scheme !== '') {
    $sectionClasses .= ' section--scheme-' . e($color_scheme);
}
$sectionSurface = $data['section_surface'] ?? '';
if ($sectionSurface !== '') {
    $sectionClasses .= ' section--surface-' . e($sectionSurface);
}
if ($sectionTextColor !== '') {
    $sectionClasses .= ' section--text-' . e($sectionTextColor);
}
?>
<section class="<?= $sectionClasses ?>" id="<?= e($sectionId) ?>">
    <div class="container">
        <?php if ($title !== ''): ?>
        <div class="video-section-header reveal">
            <h2 class="video-section-title"><?= e($title) ?></h2>
            <?php if ($subtitle !== ''): ?>
            <p class="video-section-subtitle"><?= e($subtitle) ?></p>
            <?php endif; ?>
        </div>
        <?php endif; ?>

        <div class="video-embed reveal">
            <?php if ($video_type === 'youtube'): ?>
                <?php
                $videoId = extractYouTubeId($video_url);
                if ($videoId !== ''):
                ?>
                <iframe
                    src="https://www.youtube-nocookie.com/embed/<?= e($videoId) ?>"
                    title="<?= e($title ?: 'Video') ?>"
                    allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
                    allowfullscreen>
                </iframe>
                <?php else: ?>
                <div style="display:flex; align-items:center; justify-content:center; height:100%; background:#f0f0f0; color:#666;">
                    <p>Invalid YouTube URL</p>
                </div>
                <?php endif; ?>
            <?php elseif ($video_type === 'local'): ?>
                <video
                    controls
                    <?php if ($autoplay): ?>autoplay<?php endif; ?>
                    <?php if ($poster_image !== ''): ?>poster="<?= e($poster_image) ?>"<?php endif; ?>>
                    <source src="<?= e($video_url) ?>" type="video/mp4">
                    Your browser does not support the video tag.
                </video>
            <?php endif; ?>
        </div>

        <?php if ($cta_text !== '' && $cta_url !== ''): ?>
        <div class="video-section-cta">
            <a href="<?= e($cta_url) ?>" class="btn"><?= e($cta_text) ?></a>
        </div>
        <?php endif; ?>
    </div>
</section>
