<?php

/**
 * Lightweight API wrapper for plugins in the admin context.
 * Provides read-only access to the site config (no CMS instance needed).
 */
class AdminPluginAPI implements PluginAPIInterface
{
    private array $config;
    private string $projectRoot;

    public function __construct(array $siteConfig, string $projectRoot = '')
    {
        $this->config = $siteConfig;
        $this->projectRoot = $projectRoot !== '' ? $projectRoot : dirname(__DIR__, 3);
    }

    /**
     * Get configuration value using dot notation.
     */
    public function getConfig(string $key, $default = null)
    {
        $keys = explode('.', $key);
        $value = $this->config;

        foreach ($keys as $k) {
            if (!is_array($value) || !isset($value[$k])) {
                return $default;
            }
            $value = $value[$k];
        }

        return $value;
    }

    /**
     * Get the project root directory.
     */
    public function getProjectRoot(): string
    {
        return $this->projectRoot;
    }

    /**
     * Get the content directory path.
     */
    public function getContentDir(): string
    {
        return $this->config['content_dir'] ?? ($this->projectRoot . '/content');
    }

    /**
     * Get the plugins directory path.
     */
    public function getPluginsDir(): string
    {
        return $this->projectRoot . '/plugins';
    }

    /**
     * Get the list of enabled plugins.
     */
    public function getEnabledPlugins(): array
    {
        return $this->config['enabled_plugins'] ?? [];
    }

    /**
     * Get the version info from version.php.
     */
    public function getVersionInfo(): array
    {
        $versionFile = $this->projectRoot . '/version.php';
        if (file_exists($versionFile)) {
            $data = include $versionFile;
            if (is_array($data)) {
                return $data;
            }
        }
        return ['version' => '0.0.0'];
    }
}