package example;

import forgottentowns.mod.api.GameMod;
import forgottentowns.mod.api.ModContext;
import forgottentowns.mod.api.RegistryAccess;

/** Demonstrates checking the final composed content through the supported snapshot. */
public final class MeadowLifeMod implements GameMod {
    /** Creates the public entry point loaded from the manifest. */
    public MeadowLifeMod() { }

    /**
     * Registers a startup consistency check after every pack has finished composing.
     * @param context startup registration services supplied by the game
     */
    @Override
    public void register(ModContext context) {
        context.onRegistriesReady(MeadowLifeMod::checkFoodChain);
    }

    private static void checkFoodChain(RegistryAccess registries) {
        // An add-on may rebalance nutrition. Require edible food, not our original exact value.
        var berry = registries.find("food", "meadow_life:berry").orElseThrow();
        if (!(berry.values().get("nutrition") instanceof Number nutrition) || nutrition.doubleValue() <= 0) {
            throw new IllegalStateException("Meadow Life requires an edible meadow berry");
        }
        var recipe = registries.find("recipes", "meadow_life:snack").orElseThrow();
        String output = (String) recipe.values().get("outputResourceId");
        if (registries.find("food", output).isEmpty()) {
            throw new IllegalStateException("Meadow Life snack recipe requires a food profile");
        }
    }
}
