package party.cybsec.griefdetect.core;

import org.bukkit.plugin.java.JavaPlugin;
import party.cybsec.griefdetect.GriefDetectPlugin;
import party.cybsec.griefdetect.config.PluginConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Module manager for registering and managing detection modules.
 * Modules are enabled/disabled via config and register listeners only if enabled.
 */
public class ModuleManager {
    
    private final GriefDetectPlugin plugin;
    private final PluginConfig config;
    private final Map<String, DetectionModule> modules;
    
    public ModuleManager(GriefDetectPlugin plugin) {
        this.plugin = plugin;
        this.config = plugin.getPluginConfig();
        this.modules = new ConcurrentHashMap<>();
    }
    
    /**
     * Register a detection module.
     * 
     * @param module The module to register
     */
    public void registerModule(DetectionModule module) {
        String moduleName = module.getModuleName();
        
        if (config.isModuleEnabled(moduleName)) {
            modules.put(moduleName, module);
            module.initialize();
            plugin.getLogger().info("Module registered and enabled: " + moduleName);
        } else {
            plugin.getLogger().info("Module registered but disabled: " + moduleName);
        }
    }
    
    /**
     * Get a registered module by name.
     * 
     * @param moduleName The name of the module
     * @return The module if found, null otherwise
     */
    public DetectionModule getModule(String moduleName) {
        return modules.get(moduleName);
    }
    
    /**
     * Get all registered modules.
     * 
     * @return List of all registered modules
     */
    public List<DetectionModule> getAllModules() {
        return new ArrayList<>(modules.values());
    }
    
    /**
     * Reload all modules.
     * Called on plugin reload.
     */
    public void reloadModules() {
        for (DetectionModule module : modules.values()) {
            module.reload();
        }
        plugin.getLogger().info("All modules reloaded");
    }
    
    /**
     * Shutdown all modules.
     * Called on plugin disable.
     */
    public void shutdownModules() {
        for (DetectionModule module : modules.values()) {
            module.shutdown();
        }
        modules.clear();
        plugin.getLogger().info("All modules shutdown");
    }
    
    /**
     * Interface for detection modules.
     */
    public interface DetectionModule {
        /**
         * Get the module name.
         */
        String getModuleName();
        
        /**
         * Initialize the module.
         * Register listeners and set up configuration.
         */
        void initialize();
        
        /**
         * Reload the module configuration.
         */
        void reload();
        
        /**
         * Shutdown the module.
         * Unregister listeners and cleanup resources.
         */
        void shutdown();
        
        /**
         * Get the module's configuration subtree.
         */
        Map<String, Object> getModuleConfig();
    }
}