<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\VideoPlugin;
use Illuminate\Http\Request;

class VideoPluginController extends Controller
{
    public function index()
    {
        $plugins = VideoPlugin::orderBy('order')->get();
        return view('admin.video-plugins.index', compact('plugins'));
    }

    public function install($code)
    {
        $pluginConfig = config("video_plugins.{$code}");
        if (!$pluginConfig || !class_exists($pluginConfig['class'])) {
            return redirect()->route('admin.video-plugins.index')
                ->with('error', "Video plugin '{$code}' not found.");
        }

        $existing = VideoPlugin::where('code', $code)->first();
        if ($existing) {
            $existing->update(['is_installed' => true]);
            return redirect()->route('admin.video-plugins.index')
                ->with('success', "Plugin '{$pluginConfig['name']}' installed.");
        }

        $plugin = app($pluginConfig['class']);

        VideoPlugin::create([
            'code' => $code,
            'name' => $pluginConfig['name'],
            'description' => $pluginConfig['description'],
            'version' => $plugin->getVersion(),
            'author' => $plugin->getAuthor(),
            'is_active' => false,
            'is_installed' => true,
            'config' => [],
            'order' => VideoPlugin::max('order') + 1,
        ]);

        return redirect()->route('admin.video-plugins.index')
            ->with('success', "Plugin '{$pluginConfig['name']}' installed successfully.");
    }

    public function uninstall($code)
    {
        $plugin = VideoPlugin::where('code', $code)->first();
        if (!$plugin) {
            return redirect()->route('admin.video-plugins.index')
                ->with('error', 'Plugin not found.');
        }

        $plugin->update(['is_installed' => false, 'is_active' => false]);

        return redirect()->route('admin.video-plugins.index')
            ->with('success', "Plugin '{$plugin->name}' uninstalled.");
    }

    public function edit(VideoPlugin $videoPlugin)
    {
        $resolved = $videoPlugin->resolvePlugin();
        $fields = $resolved ? $resolved->getConfigFields() : [];

        return view('admin.video-plugins.edit', compact('videoPlugin', 'fields'));
    }

    public function update(Request $request, VideoPlugin $videoPlugin)
    {
        $resolved = $videoPlugin->resolvePlugin();
        if (!$resolved) {
            return redirect()->route('admin.video-plugins.index')
                ->with('error', 'Video plugin class not found.');
        }

        $validated = $resolved->validateConfig($request->input('config', []));

        $videoPlugin->update([
            'config' => $validated,
            'is_active' => $request->boolean('is_active', false),
        ]);

        return redirect()->route('admin.video-plugins.index')
            ->with('success', "Plugin '{$videoPlugin->name}' configured.");
    }

    public function toggle(VideoPlugin $videoPlugin)
    {
        $videoPlugin->update(['is_active' => !$videoPlugin->is_active]);

        $status = $videoPlugin->is_active ? 'activated' : 'deactivated';
        return redirect()->route('admin.video-plugins.index')
            ->with('success', "Plugin '{$videoPlugin->name}' {$status}.");
    }
}
