package com.tiedup.remake.entities.ai.personality;

import com.tiedup.remake.entities.EntityDamsel;
import com.tiedup.remake.personality.NpcCommand;
import java.util.EnumSet;
import java.util.UUID;
import javax.annotation.Nullable;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.player.Player;

/**
 * AI Goal: NPC sits down when SIT command is given.
 *
 * Personality System Phase D: AI Goals
 *
 * <p><b>Behavior:</b></p>
 * <ul>
 *   <li>Only active when NPC has SIT command</li>
 *   <li>NPC enters sitting pose (stops movement)</li>
 *   <li>Awards XP on command start</li>
 *   <li>Stays sitting until command is cancelled</li>
 * </ul>
 */
public class NpcSitCommandGoal extends Goal {

    private final EntityDamsel npc;

    /** Has XP been awarded for this sit session? */
    private boolean xpAwarded;

    public NpcSitCommandGoal(EntityDamsel npc) {
        this.npc = npc;
        // Block movement and jumping while sitting
        this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.JUMP));
    }

    @Override
    public boolean canUse() {
        // Must have SIT command active
        NpcCommand activeCmd = npc.getActiveCommand();
        if (activeCmd != NpcCommand.SIT) {
            return false;
        }

        // Must have collar
        if (!npc.hasCollar()) {
            com.tiedup.remake.core.TiedUpMod.LOGGER.debug(
                "[NpcSitCommandGoal] canUse=false: NPC {} has SIT command but no collar",
                npc.getNpcName()
            );
            return false;
        }

        com.tiedup.remake.core.TiedUpMod.LOGGER.debug(
            "[NpcSitCommandGoal] canUse=true for NPC {}",
            npc.getNpcName()
        );
        return true;
    }

    @Override
    public boolean canContinueToUse() {
        // Stop if command changed
        if (npc.getActiveCommand() != NpcCommand.SIT) {
            return false;
        }

        // Stop if no collar
        if (!npc.hasCollar()) {
            return false;
        }

        return true;
    }

    @Override
    public void start() {
        this.xpAwarded = false;

        // Stop all movement
        this.npc.getNavigation().stop();

        // Set sitting pose
        this.npc.setSitting(true);

        com.tiedup.remake.core.TiedUpMod.LOGGER.debug(
            "[NpcSitCommandGoal] STARTED - NPC {} is now sitting (isSitting={})",
            npc.getNpcName(),
            npc.isSitting()
        );

        // Award XP for obeying
        awardXP();
    }

    @Override
    public void stop() {
        // Clear sitting pose
        this.npc.setSitting(false);
    }

    @Override
    public void tick() {
        // Ensure we stay still
        if (this.npc.getNavigation().isInProgress()) {
            this.npc.getNavigation().stop();
        }

        // Award XP if not already done
        if (!this.xpAwarded) {
            awardXP();
        }
    }

    @Override
    public boolean requiresUpdateEveryTick() {
        return true;
    }

    /**
     * Award XP to the commanding player.
     */
    private void awardXP() {
        if (this.xpAwarded) return;

        var state = this.npc.getPersonalityState();
        if (state == null) return;

        UUID commanderUUID = state.getCommandingPlayer();
        if (commanderUUID == null) return;

        for (Player player : this.npc.level().players()) {
            if (player.getUUID().equals(commanderUUID)) {
                this.xpAwarded = true;
                break;
            }
        }
    }
}
