<?php
/**
 * ITSWEBER CMS – Video Section
 *
 * YouTube or local video embed with poster image.
 * Migriert aus plugins/video-section.
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

namespace App\Sections\Extended;

use App\Sections\AbstractSection;
use App\Core\Request;

class VideoSection extends AbstractSection
{
    public static function typeKey(): string    { return 'video_section'; }
    public static function label(): string      { return 'Video'; }
    public static function category(): string   { return 'media'; }
    public static function description(): string { return 'YouTube- oder lokales Video mit Poster-Bild'; }

    public static function iconSvg(): string
    {
        return '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">'
             . '<rect x="2" y="4" width="20" height="16" rx="2"/>'
             . '<polygon points="10 8 16 12 10 16" fill="currentColor"/>'
             . '</svg>';
    }

    public function buildData(Request $request): array
    {
        $allowedVideoType = ['youtube', 'local'];

        $videoType = (string) ($request->input('video_type', 'youtube') ?? '');
        if (!in_array($videoType, $allowedVideoType, true)) {
            $videoType = 'youtube';
        }

        $ctaUrl = trim((string) ($request->input('cta_url', '') ?? ''));
        if ($ctaUrl !== '' && !preg_match('/^(https?:\/\/|\/)/i', $ctaUrl)) {
            $ctaUrl = '';
        }

        $data = [
            'title'              => trim((string) ($request->input('title', '') ?? '')),
            'subtitle'           => trim((string) ($request->input('subtitle', '') ?? '')),
            'video_type'         => $videoType,
            'video_url'          => trim((string) ($request->input('video_url', '') ?? '')),
            'poster_image'       => trim((string) ($request->input('poster_image', '') ?? '')),
            'autoplay'           => (int) ($request->input('autoplay', 0) ?? 0) ? 1 : 0,
            'cta_text'           => trim((string) ($request->input('cta_text', '') ?? '')),
            'cta_url'            => $ctaUrl,
            'color_scheme'       => self::sanitizeToken((string) ($request->input('color_scheme', '') ?? ''), self::allowedColorScheme()),
            'section_text_color' => self::sanitizeToken((string) ($request->input('section_text_color', '') ?? ''), self::allowedSectionTextColor()),
        ];

        return $this->mergeThemeExtras($data, $request);
    }

    public function defaultData(): array
    {
        return [
            'title' => 'Video',
            'subtitle' => 'Sehen Sie selbst',
            'video_type' => 'youtube', 'video_url' => '',
            'poster_image' => '', 'autoplay' => 0,
            'cta_text' => '', 'cta_url' => '',
            'color_scheme' => '', 'section_text_color' => '',
        ];
    }

    public function styles(): array
    {
        return ['/assets/css/sections/video-section.css'];
    }

    /**
     * Extract YouTube video ID from various URL formats.
     * Ported from plugins/video-section/plugin.php.
     */
    public static function extractYouTubeId(string $url): string
    {
        if (preg_match('/(?:youtube\.com\/(?:watch\?v=|embed\/)|youtu\.be\/)([\w-]{11})/', $url, $m)) {
            return $m[1];
        }
        return '';
    }
}
