package party.cybsec.griefdetect;

import org.bukkit.plugin.java.JavaPlugin;
import party.cybsec.griefdetect.core.ModuleManager;
import party.cybsec.griefdetect.core.WebhookManager;
import party.cybsec.griefdetect.core.PermissionManager;
import party.cybsec.griefdetect.core.ConfidenceEngine;
import party.cybsec.griefdetect.core.DetectionEngine;
import party.cybsec.griefdetect.core.ChatAlertManager;
import party.cybsec.griefdetect.modules.LavaCastDetector;
import party.cybsec.griefdetect.config.PluginConfig;
import party.cybsec.griefdetect.commands.ReloadCommand;

/**
 * Main plugin class for GriefDetect.
 * Modular, async-first grief pattern detection with confidence scoring and Discord webhook reporting.
 */
public class GriefDetectPlugin extends JavaPlugin {
    
    private static GriefDetectPlugin instance;
    
    private PluginConfig config;
    private ModuleManager moduleManager;
    private WebhookManager webhookManager;
    private PermissionManager permissionManager;
    private ConfidenceEngine confidenceEngine;
    private DetectionEngine detectionEngine;
    
    @Override
    public void onEnable() {
        instance = this;
        
        // Initialize core systems
        initializeConfig();
        initializeCoreSystems();
        initializeModules();
        registerCommands();
        
        getLogger().info("GriefDetect enabled - " + getDescription().getVersion());
    }
    
    @Override
    public void onDisable() {
        // Cleanup resources
        if (detectionEngine != null) {
            detectionEngine.shutdown();
        }
        
        getLogger().info("GriefDetect disabled");
    }
    
    private void initializeConfig() {
        config = new PluginConfig(this);
        config.load();
    }
    
    private void initializeCoreSystems() {
        permissionManager = new PermissionManager(this);
        confidenceEngine = new ConfidenceEngine(this);
        webhookManager = new WebhookManager(this);
        detectionEngine = new DetectionEngine(this);
        moduleManager = new ModuleManager(this);
        // Note: ChatAlertManager is created by DetectionEngine when needed
    }
    
    private void initializeModules() {
        // Register modules
        moduleManager.registerModule(new LavaCastDetector(this));
        
        // Start the detection engine
        detectionEngine.start();
    }
    
    private void registerCommands() {
        getCommand("griefdetect").setExecutor(new ReloadCommand(this));
    }
    
    // Getters for accessing core systems
    public static GriefDetectPlugin getInstance() {
        return instance;
    }
    
    public PluginConfig getPluginConfig() {
        return config;
    }
    
    public ModuleManager getModuleManager() {
        return moduleManager;
    }
    
    public WebhookManager getWebhookManager() {
        return webhookManager;
    }
    
    public PermissionManager getPermissionManager() {
        return permissionManager;
    }
    
    public ConfidenceEngine getConfidenceEngine() {
        return confidenceEngine;
    }
    
    public DetectionEngine getDetectionEngine() {
        return detectionEngine;
    }
    
    public ChatAlertManager getChatAlertManager() {
        return new ChatAlertManager(this);
    }
}
