<?php

namespace App\VideoPlugins;

use App\Models\VideoPlugin;
use App\VideoPlugins\Contracts\BaseVideoPlugin;

class VimeoVideo extends BaseVideoPlugin
{
    public function getCode(): string
    {
        return 'vimeo';
    }

    public function getName(): string
    {
        return 'Vimeo Video';
    }

    public function getDescription(): string
    {
        return 'Embed Vimeo videos with responsive iframe player. Supports private and public video embeds.';
    }

    public function getConfigFields(): array
    {
        return [
            [
                'key' => 'default_width',
                'label' => 'Default Embed Width',
                'type' => 'text',
                'placeholder' => '640',
            ],
            [
                'key' => 'default_height',
                'label' => 'Default Embed Height',
                'type' => 'text',
                'placeholder' => '360',
            ],
        ];
    }

    public function validateConfig(array $config): array
    {
        return [
            'default_width' => trim($config['default_width'] ?? '640'),
            'default_height' => trim($config['default_height'] ?? '360'),
        ];
    }

    public function isConfigured(VideoPlugin $plugin): bool
    {
        return true;
    }

    public function renderEmbed(VideoPlugin $plugin, string $url = ''): string
    {
        if (!$url) return '';

        $videoId = $this->extractVideoId($url);
        if (!$videoId) return '';

        return <<<HTML
<div style="position:relative;padding-bottom:56.25%;height:0;overflow:hidden;max-width:100%;background:#000">
    <iframe src="https://player.vimeo.com/video/{$videoId}" 
        style="position:absolute;top:0;left:0;width:100%;height:100%" 
        frameborder="0" allowfullscreen
        allow="autoplay;fullscreen;picture-in-picture">
    </iframe>
</div>
HTML;
    }

    protected function extractVideoId(string $url): ?string
    {
        if (preg_match('/vimeo\.com\/(\d+)/', $url, $m)) {
            return $m[1];
        }
        return null;
    }
}
