<?php

class PluginManager
{
    private array $plugins = [];
    private string $pluginsPath;
    private ?CMSAPI $api = null;

    public function __construct(string $pluginsPath)
    {
        $this->pluginsPath = $pluginsPath;
        $this->loadPlugins();
    }
    
    public function setAPI(CMSAPI $api): void
    {
        $this->api = $api;
        
        foreach ($this->plugins as $plugin) {
            if (method_exists($plugin, 'setAPI')) {
                $plugin->setAPI($api);
            }
        }
    }

    private function loadPlugins(): void
    {
        if (!is_dir($this->pluginsPath)) {
            return;
        }

        $pluginDirs = glob($this->pluginsPath . '/*', GLOB_ONLYDIR);
        
        foreach ($pluginDirs as $pluginDir) {
            $pluginName = basename($pluginDir);
            $pluginFile = $pluginDir . '/' . $pluginName . '.php';
            
            if (file_exists($pluginFile)) {
                require_once $pluginFile;
                
                $className = $pluginName;
                if (class_exists($className)) {
                    $this->plugins[$pluginName] = new $className();
                    
                    if ($this->api && method_exists($this->plugins[$pluginName], 'setAPI')) {
                        $this->plugins[$pluginName]->setAPI($this->api);
                    }
                }
            }
        }
    }

    public function getPlugin(string $name): ?object
    {
        return $this->plugins[$name] ?? null;
    }

    public function getAllPlugins(): array
    {
        return $this->plugins;
    }

    public function isPluginViewable(object $plugin): bool
    {
        if (method_exists($plugin, 'getConfig')) {
            $config = $plugin->getConfig();
            return !isset($config['viewable']) || $config['viewable'] !== false;
        }
        return true;
    }

    public function getSidebarContent(?array $allowedPlugins = null): string
    {
        $sidebarContent = '';
        
        foreach ($this->plugins as $pluginName => $plugin) {
            if (!$this->isPluginViewable($plugin) || !method_exists($plugin, 'getSidebarContent')) {
                continue;
            }
            
            // Filter by allowed plugins for this page
            if ($allowedPlugins !== null && !in_array($pluginName, $allowedPlugins, true)) {
                continue;
            }
            
            $content = $plugin->getSidebarContent();
            if (trim($content) === '') {
                continue;
            }
            
            $title = 'Plugin';
            if (method_exists($plugin, 'getConfig')) {
                $config = $plugin->getConfig();
                $title = $config['title'] ?? 'Plugin';
            }
            
            $sidebarContent .= '
                <div class="card mb-3">
                    <div class="card-header">
                        <h5 class="mb-0">' . htmlspecialchars($title) . '</h5>
                    </div>
                    <div class="card-body">
                        ' . $content . '
                    </div>
                </div>';
        }
        
        return $sidebarContent;
    }
}