<?php

declare(strict_types=1);

namespace WPStack\Sandbox\Domain;

final class PluginCatalog
{
    /**
     * @var array<string, array{name:string,description:string,file:string,landing:string,accent:string}>
     */
    private array $plugins;

    /**
     * @param list<array<string, mixed>>|null $products
     */
    public function __construct(?array $products = null)
    {
        if ($products === null) {
            $this->plugins = self::defaults();
            return;
        }

        $this->plugins = [];
        foreach ($products as $input) {
            try {
                $product = ProductDefinition::fromArray($input);
            } catch (\InvalidArgumentException) {
                continue;
            }
            if (!$product['enabled']) {
                continue;
            }
            $slug = $product['slug'];
            unset($product['slug']);
            $this->plugins[$slug] = $product;
        }
    }

    /**
     * @return array<string, array<string, mixed>>
     */
    public static function defaults(): array
    {
        return [
        'mediachecker' => [
            'name' => 'Mediachecker',
            'description' => 'Find unused, duplicate, and oversized media safely.',
            'file' => 'Mediachecker/mediachecker.php',
            'landing' => '/wp-admin/upload.php?page=mediachecker',
            'accent' => '#ff6b35',
        ],
        'autoload-manager' => [
            'name' => 'Autoloaded Options Manager',
            'description' => 'Inspect and reduce expensive autoloaded options.',
            'file' => 'autoloaded-options-manager/autoloaded-options-manager.php',
            'landing' => '/wp-admin/tools.php?page=autoloaded-options-manager',
            'accent' => '#7c3aed',
        ],
        'top-load-monitor' => [
            'name' => 'WPStack TOP Load Monitor',
            'description' => 'Understand slow WordPress requests and bottlenecks.',
            'file' => 'wptop-plugin/wptop.php',
            'landing' => '/wp-admin/admin.php?page=wpstack-top',
            'accent' => '#2563eb',
        ],
        'menoads' => [
            'name' => 'MeNoAds',
            'description' => 'Remove intrusive WordPress admin notices.',
            'file' => 'MeNoAds/me-no-ads.php',
            'landing' => '/wp-admin/options-general.php?page=meno-ads',
            'accent' => '#059669',
        ],
        'ai-alt-genius' => [
            'name' => 'AI ALT Genius',
            'description' => 'Generate better image alt text from the Media Library.',
            'file' => 'wpstack-ai-alt-genius/wpstack-ai-alt-genius.php',
            'landing' => '/wp-admin/admin.php?page=wpstack-ai-alt-genius',
            'accent' => '#db2777',
        ],
        'editor-optimizer' => [
            'name' => 'Miracuves Editor Optimizer',
            'description' => 'Improve the Elementor editing experience and performance.',
            'file' => 'miracuves-editor-optimizer/miracuves-editor-optimizer.php',
            'landing' => '/wp-admin/options-general.php?page=meo-miracuves',
            'accent' => '#d97706',
            'dependencies' => ['elementor/elementor.php'],
        ],
        ];
    }

    /**
     * @return array<string, array{name:string,description:string,file:string,landing:string,accent:string}>
     */
    public function all(): array
    {
        return $this->plugins;
    }

    public function has(string $slug): bool
    {
        return isset($this->plugins[$slug]);
    }

    /**
     * @return array{name:string,description:string,file:string,landing:string,accent:string}|null
     */
    public function get(string $slug): ?array
    {
        return $this->plugins[$slug] ?? null;
    }

    /**
     * @return list<string>
     */
    public function files(): array
    {
        return array_values(array_column($this->plugins, 'file'));
    }
}
