package com.tiedup.remake.entities.kidnapper.components;

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityKidnapper;
import com.tiedup.remake.entities.ai.kidnapper.*;
import net.minecraft.world.entity.ai.goal.*;
import net.minecraft.world.entity.player.Player;

/**
 * KidnapperAIManager - Manages AI goal registration.
 *
 * Handles:
 * - Registering all 20 AI goals with priorities
 * - Does NOT call super() - kidnappers have completely different AI than damsels
 *
 * <p><b>Low complexity</b> - Straightforward goal registration.</p>
 */
public class KidnapperAIManager {

    // FIELDS

    /** Entity reference (needed for goal constructors) */
    private final EntityKidnapper entity;

    /** Host callbacks */
    private final IAIHost host;

    // CONSTRUCTOR

    public KidnapperAIManager(EntityKidnapper entity, IAIHost host) {
        this.entity = entity;
        this.host = host;
    }

    // AI GOAL REGISTRATION

    /**
     * Register all AI goals for the kidnapper.
     * CRITICAL: Does NOT call super() - kidnappers have completely different AI than damsels.
     */
    public void registerGoals() {
        // Don't call super - kidnapper has completely different AI than damsel

        // Priority 0: Always swim
        host.getGoalSelector().addGoal(0, new FloatGoal(this.entity));

        // Priority 1: Fight back if attacked while holding captive (MEDIUM FIX: higher priority - self defense)
        host
            .getGoalSelector()
            .addGoal(1, new KidnapperFightBackGoal(this.entity));

        // Priority 1: Self-defense when attacked without captives (prevents PROTECTED exploit)
        host
            .getGoalSelector()
            .addGoal(1, new KidnapperSelfDefenseGoal(this.entity));

        // Priority 2: Hunt monsters (camp kidnappers only - protect captives)
        host
            .getGoalSelector()
            .addGoal(2, new KidnapperHuntMonstersGoal(this.entity));

        // Priority 3: ALERT - Search for escaped prisoner (high priority) (MEDIUM FIX: bumped from 2 to 3)
        host.getGoalSelector().addGoal(3, new KidnapperAlertGoal(this.entity));

        // Priority 3: Find and capture targets
        host
            .getGoalSelector()
            .addGoal(3, new KidnapperFindTargetGoal(this.entity, 25));

        // Priority 3: PUNISH - Punish recaptured prisoner (after Alert, before Capture)
        host.getGoalSelector().addGoal(3, new KidnapperPunishGoal(this.entity));

        // Priority 4: Capture target
        host
            .getGoalSelector()
            .addGoal(4, new KidnapperCaptureGoal(this.entity));

        // Priority 4: Recapture escaped captives (same priority as capture, but checked after)
        host
            .getGoalSelector()
            .addGoal(4, new KidnapperRecaptureGoal(this.entity));

        // Priority 5: Bring slave to cell (unified goal handles both camp and player cells)
        host
            .getGoalSelector()
            .addGoal(5, new KidnapperBringToCellGoal(this.entity));

        // Priority 6: Decide what to do with slave (sell or job)
        host
            .getGoalSelector()
            .addGoal(6, new KidnapperDecideNextActionGoal(this.entity));

        // Priority 6: Dispersal - Force movement away when too many near camp center
        host
            .getGoalSelector()
            .addGoal(6, new KidnapperDispersalGoal(this.entity));

        // Priority 7: GUARD - Watch over occupied cells
        host.getGoalSelector().addGoal(7, new KidnapperGuardGoal(this.entity));

        // Priority 7: Thief behavior for wild kidnappers (steal items and flee)
        // BUG FIX: DISABLED - Integrated into DecideNextActionGoal
        // Problem: ThiefGoal (P7) never executed because DecideNextActionGoal (P6)
        // finished first and changed state (sell/job), blocking ThiefGoal activation.
        // Solution: Integrated theft as a 3rd option (30% chance) in DecideNextActionGoal.
        // The standalone ThiefGoal is now DEAD CODE and has been disabled.
        // host.getGoalSelector().addGoal(7, new KidnapperThiefGoal(this.entity));

        // Priority 7: Walk prisoner (DISABLED - chunk unload loses goal state,
        // causing prisoners to be sold/stolen instead of returned to cell)
        // host.getGoalSelector().addGoal(7, new KidnapperWalkPrisonerGoal(this.entity));

        // Priority 8: Patrol when no slave (close to camp)
        host.getGoalSelector().addGoal(8, new KidnapperPatrolGoal(this.entity));

        // Priority 8: Hunt far from camp (only for hunters)
        host.getGoalSelector().addGoal(8, new KidnapperHuntGoal(this.entity));

        // Priority 9-10: Flee behavior (getOutState)
        host
            .getGoalSelector()
            .addGoal(9, new KidnapperFleeWithCaptiveGoal(this.entity));
        host
            .getGoalSelector()
            .addGoal(10, new KidnapperFleeSafeGoal(this.entity));

        // Priority 11-12: Sale and Job systems
        host
            .getGoalSelector()
            .addGoal(11, new KidnapperWaitForBuyerGoal(this.entity));
        host
            .getGoalSelector()
            .addGoal(12, new KidnapperWaitForJobGoal(this.entity));

        // Priority 13-15: Basic movement (lower priority)
        host
            .getGoalSelector()
            .addGoal(13, new WaterAvoidingRandomStrollGoal(this.entity, 1.0D));
        // Use custom LookAtPlayerGoal that ignores players in cells
        host
            .getGoalSelector()
            .addGoal(
                14,
                new KidnapperLookAtPlayerGoal(this.entity, Player.class, 8.0F)
            );
        host
            .getGoalSelector()
            .addGoal(15, new RandomLookAroundGoal(this.entity));

        // Priority 16: Open doors
        host
            .getGoalSelector()
            .addGoal(16, new OpenDoorGoal(this.entity, false));

        TiedUpMod.LOGGER.debug(
            "[EntityKidnapper] Registered {} AI goals (ThiefGoal disabled)",
            20
        );
    }
}
