<?php
namespace WooNooW\Compat;

if ( ! defined('ABSPATH') ) exit;

/**
 * Addon Registry
 * 
 * Central registry for WooNooW addons. Collects metadata, validates dependencies,
 * and exposes addon information to the SPA.
 * 
 * @since 1.0.0
 */
class AddonRegistry {
    const REGISTRY_OPTION = 'wnw_addon_registry';
    const REGISTRY_VERSION = '1.0.0';
    
    /**
     * Initialize hooks
     */
    public static function init() {
        add_action('plugins_loaded', [__CLASS__, 'collect_addons'], 20);
        add_action('activated_plugin', [__CLASS__, 'flush']);
        add_action('deactivated_plugin', [__CLASS__, 'flush']);
    }
    
    /**
     * Collect and validate addons
     */
    public static function collect_addons() {
        // Start with empty registry
        $addons = [];
        
        /**
         * Filter: woonoow/addon_registry
         * 
         * Allows addons to register themselves with WooNooW.
         * 
         * @param array $addons Array of addon configurations
         * 
         * Example:
         * add_filter('woonoow/addon_registry', function($addons) {
         *     $addons['my-addon'] = [
         *         'id'           => 'my-addon',
         *         'name'         => 'My Addon',
         *         'version'      => '1.0.0',
         *         'author'       => 'Author Name',
         *         'description'  => 'Addon description',
         *         'spa_bundle'   => plugin_dir_url(__FILE__) . 'dist/addon.js',
         *         'dependencies' => ['woocommerce' => '8.0'],
         *         'routes'       => [],
         *         'nav_items'    => [],
         *         'widgets'      => [],
         *     ];
         *     return $addons;
         * });
         */
        $addons = apply_filters('woonoow/addon_registry', $addons);
        
        // Validate and normalize each addon
        $validated = [];
        foreach ($addons as $id => $addon) {
            $validated[$id] = self::validate_addon($id, $addon);
        }
        
        // Store in option
        update_option(self::REGISTRY_OPTION, [
            'version' => self::REGISTRY_VERSION,
            'addons'  => $validated,
            'updated' => time(),
        ], false);
    }
    
    /**
     * Validate and normalize addon configuration
     * 
     * @param string $id Addon ID
     * @param array $addon Addon configuration
     * @return array Validated addon configuration
     */
    private static function validate_addon(string $id, array $addon): array {
        $defaults = [
            'id'           => $id,
            'name'         => $id,
            'version'      => '1.0.0',
            'author'       => '',
            'description'  => '',
            'spa_bundle'   => '',
            'dependencies' => [],
            'routes'       => [],
            'nav_items'    => [],
            'widgets'      => [],
            'enabled'      => true,
        ];
        
        $validated = wp_parse_args($addon, $defaults);
        
        // Ensure ID matches
        $validated['id'] = $id;
        
        // Validate dependencies
        $validated['dependencies_met'] = self::check_dependencies($validated['dependencies']);
        
        // If dependencies not met, disable addon
        if (!$validated['dependencies_met']) {
            $validated['enabled'] = false;
        }
        
        return $validated;
    }
    
    /**
     * Check if addon dependencies are met
     * 
     * @param array $dependencies Array of plugin => version requirements
     * @return bool True if all dependencies met
     */
    private static function check_dependencies(array $dependencies): bool {
        foreach ($dependencies as $plugin => $required_version) {
            // Check WooCommerce
            if ($plugin === 'woocommerce') {
                if (!defined('WC_VERSION')) {
                    return false;
                }
                if (version_compare(WC_VERSION, $required_version, '<')) {
                    return false;
                }
            }
            
            // Check WordPress
            if ($plugin === 'wordpress') {
                global $wp_version;
                if (version_compare($wp_version, $required_version, '<')) {
                    return false;
                }
            }
            
            // Check other plugins (basic check)
            if (!in_array($plugin, ['woocommerce', 'wordpress'])) {
                if (!is_plugin_active($plugin)) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    /**
     * Get all registered addons
     * 
     * @param bool $enabled_only Return only enabled addons
     * @return array Array of addon configurations
     */
    public static function get_addons(bool $enabled_only = false): array {
        $data = get_option(self::REGISTRY_OPTION, []);
        $addons = $data['addons'] ?? [];
        
        if ($enabled_only) {
            $addons = array_filter($addons, function($addon) {
                return !empty($addon['enabled']);
            });
        }
        
        return $addons;
    }
    
    /**
     * Get a specific addon
     * 
     * @param string $id Addon ID
     * @return array|null Addon configuration or null if not found
     */
    public static function get_addon(string $id): ?array {
        $addons = self::get_addons();
        return $addons[$id] ?? null;
    }
    
    /**
     * Check if an addon is registered and enabled
     * 
     * @param string $id Addon ID
     * @return bool True if addon is registered and enabled
     */
    public static function is_addon_enabled(string $id): bool {
        $addon = self::get_addon($id);
        return $addon && !empty($addon['enabled']);
    }
    
    /**
     * Flush the registry cache
     */
    public static function flush() {
        delete_option(self::REGISTRY_OPTION);
    }
    
    /**
     * Get registry for frontend (sanitized)
     * 
     * @return array Array suitable for JSON encoding
     */
    public static function get_frontend_registry(): array {
        $addons = self::get_addons(true); // Only enabled
        
        $frontend = [];
        foreach ($addons as $id => $addon) {
            $frontend[$id] = [
                'id'          => $addon['id'],
                'name'        => $addon['name'],
                'version'     => $addon['version'],
                'author'      => $addon['author'],
                'description' => $addon['description'],
                'spa_bundle'  => $addon['spa_bundle'],
                // Don't expose internal data
            ];
        }
        
        return $frontend;
    }
}
