<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class VideoPlugin extends Model
{
    protected $table = 'video_plugins';

    protected $fillable = [
        'code', 'name', 'description', 'version', 'author',
        'is_active', 'is_installed', 'config', 'order',
    ];

    protected $casts = [
        'config' => 'array',
        'is_active' => 'boolean',
        'is_installed' => 'boolean',
        'order' => 'integer',
    ];

    public function scopeActive($query)
    {
        return $query->where('is_active', true)->where('is_installed', true);
    }

    public function scopeOrdered($query)
    {
        return $query->orderBy('order');
    }

    public function resolvePlugin(): ?\App\VideoPlugins\Contracts\VideoPluginInterface
    {
        $class = config("video_plugins.{$this->code}.class");
        if (!$class || !class_exists($class)) {
            return null;
        }
        return app($class);
    }
}
