package com.tiedup.remake.compat.mca.ai.chatai;

import com.tiedup.remake.compat.mca.MCACompat;
import com.tiedup.remake.compat.mca.personality.MCAPersonalityManager;
import com.tiedup.remake.compat.mca.personality.TiedUpTrait;
import com.tiedup.remake.state.IBondageState;
import com.tiedup.remake.state.ICaptor;
import java.util.List;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;

/**
 * AI Context Module for MCA's OpenAI chat system.
 *
 * <p>Injects TiedUp bondage state into the AI context so the LLM
 * can respond appropriately when a villager is restrained.
 *
 * <p>Similar to MCA's PersonalityModule, TraitsModule, etc.
 * This module adds contextual information about:
 * <ul>
 *   <li>Current bondage state (tied, gagged, blindfolded, collared)</li>
 *   <li>TiedUp trait (MASO, REBELLIOUS, BROKEN, TRAINED)</li>
 *   <li>Captor relationship</li>
 * </ul>
 */
public class TiedUpModule {

    /**
     * Apply TiedUp context to the AI input list.
     *
     * @param input The list of context strings being built
     * @param villager The MCA villager entity
     * @param player The player interacting with the villager
     */
    public static void apply(
        List<String> input,
        LivingEntity villager,
        Player player
    ) {
        IBondageState state = MCACompat.getKidnappedState(villager);
        if (state == null) return;

        // Skip if no bondage state
        if (
            !state.isTiedUp() &&
            !state.hasCollar() &&
            !state.isGagged() &&
            !state.isBlindfolded()
        ) {
            return;
        }

        // Current bondage state
        if (state.isTiedUp()) {
            input.add("$villager is currently tied up and restrained. ");

            if (state.hasArmsBound() && state.hasLegsBound()) {
                input.add(
                    "$villager cannot move freely - both arms and legs are bound. "
                );
            } else if (state.hasArmsBound()) {
                input.add("$villager's arms are bound behind their back. ");
            } else if (state.hasLegsBound()) {
                input.add("$villager's legs are bound together. ");
            }
        }

        if (state.isGagged()) {
            input.add(
                "$villager is gagged and cannot speak clearly - only muffled sounds come out. "
            );
        }

        if (state.isBlindfolded()) {
            input.add(
                "$villager is blindfolded and cannot see anything around them. "
            );
        }

        if (state.hasCollar()) {
            input.add(
                "$villager is wearing a collar, marking them as someone's property. "
            );
        }

        // TiedUp Trait affects personality/behavior
        TiedUpTrait trait = MCAPersonalityManager.getInstance().getTrait(
            villager
        );
        switch (trait) {
            case MASO -> input.add(
                "$villager secretly enjoys being restrained and may respond positively to bondage. "
            );
            case REBELLIOUS -> input.add(
                "$villager is extremely rebellious and defiant - they will never willingly submit. "
            );
            case BROKEN -> input.add(
                "$villager has been broken psychologically and shows no resistance, responding with empty compliance. "
            );
            case TRAINED -> input.add(
                "$villager has been trained to be obedient and accepts their situation calmly. "
            );
            default -> {
                // NONE - no special trait context
            }
        }

        // Captor relationship
        if (state.isCaptive()) {
            ICaptor captor = state.getCaptor();
            if (captor != null && player != null) {
                // Use getEntity() - returns Entity, not LivingEntity
                var captorEntity = captor.getEntity();
                if (
                    captorEntity != null &&
                    captorEntity.getUUID().equals(player.getUUID())
                ) {
                    input.add(
                        "$player is $villager's captor who tied them up. "
                    );
                } else {
                    input.add("$villager has been captured by someone else. ");
                }
            } else {
                input.add("$villager has been captured by someone else. ");
            }
        }
    }
}
