<?php

namespace Core\App\Controllers\Admin;

use Core\System\PluginManager;

class PluginController
{
    public function index()
    {
        $plugins = PluginManager::all();

        require ROOT_PATH . '/admin/templates/plugins/index.php';
    }

    /**
     * Enable plugin
     */
    public function enable(string $slug)
    {
        \Core\System\Csrf::check();
        PluginManager::enable($slug);

        header('Location: /admin/plugins');
        exit;
    }

    /**
     * Disable plugin
     */
    public function disable(string $slug)
    {
        \Core\System\Csrf::check();
        PluginManager::disable($slug);

        header('Location: /admin/plugins');
        exit;
    }

    /**
     * Install plugin from ZIP
     */
    public function install()
    {
        \Core\System\Csrf::check();
        try {
            PluginManager::installFromZip($_FILES['plugin'] ?? []);
            $_SESSION['success'] = __('admin.plugin_installed', 'Plugin installed successfully');
        } catch (\Throwable $e) {
            $_SESSION['error'] = $e->getMessage();
        }

        header('Location: /admin/plugins');
        exit;
    }
}
