package com.firefrostgaming.bitchbot;

import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.server.ServerStartedEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Bitch Bot — first-boot server provisioning mod for Firefrost Gaming.
 *
 * Named by Holly (The Catalyst). Architecture locked via 4-round Gemini
 * consultation: see firefrost-operations-manual/docs/consultations/
 * gemini-modpack-installer-followup-2026-04-15.md
 *
 * Lifecycle:
 *   1. Server boots, mod loads.
 *   2. ServerStartedEvent fires.
 *   3. If ./provision.json is missing → log + exit silently. The bot is dormant.
 *   4. If present → hand off to {@link Provisioner}, which runs every step
 *      sequentially. Failures are logged but never crash the server.
 *   5. If self_destruct_on_success is true and provisioning succeeded, rename
 *      our own jar to .jar.disabled so this never runs again.
 */
@Mod(BitchBot.MODID)
public class BitchBot {
    public static final String MODID = "bitchbot";
    public static final Logger LOGGER = LoggerFactory.getLogger("BitchBot");

    public BitchBot(IEventBus modEventBus, ModContainer modContainer) {
        NeoForge.EVENT_BUS.register(this);
        LOGGER.info("Bitch Bot loaded — waiting for ServerStartedEvent.");
    }

    @SubscribeEvent
    public void onServerStarted(ServerStartedEvent event) {
        try {
            new Provisioner(event.getServer()).run();
        } catch (Throwable t) {
            // Last-resort guard — Provisioner already swallows individual step
            // failures, but if a NoClassDefFoundError or similar escapes here,
            // we MUST NOT take down the server.
            LOGGER.error("[BitchBot] Fatal provisioning error (server NOT crashed):", t);
        }
    }
}
