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

import com.tiedup.remake.core.TiedUpMod;
import java.util.UUID;
import org.jetbrains.annotations.Nullable;

/**
 * KidnapperCampManager - Manages camp/structure association.
 *
 * Handles:
 * - Structure UUID tracking (which camp this kidnapper belongs to)
 * - Hunter role assignment (patrols far vs stays close to camp)
 *
 * <p><b>Low complexity</b> - Simple state tracking with smart initialization.</p>
 */
public class KidnapperCampManager {

    // FIELDS

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

    /** UUID of the structure this kidnapper belongs to (for marker queries) */
    @Nullable
    private UUID associatedStructure;

    /**
     * Whether this kidnapper is a "hunter" that patrols far from camp.
     * Hunters patrol 80-150 blocks from camp looking for prey.
     * Non-hunters stay closer (GUARD/PATROL within 40 blocks).
     * Decided randomly when associated with a camp (50/50 chance).
     */
    private boolean isHunter = false;

    // CONSTRUCTOR

    public KidnapperCampManager(ICampHost host) {
        this.host = host;
    }

    // STRUCTURE ASSOCIATION

    /**
     * Get the associated structure UUID for this kidnapper.
     *
     * @return The structure UUID, or null if not associated
     */
    @Nullable
    public UUID getAssociatedStructure() {
        return this.associatedStructure;
    }

    /**
     * Set the associated structure UUID.
     * When first associated with a camp, randomly decides if this kidnapper
     * becomes a "hunter" (50% chance) that patrols far from camp.
     *
     * @param structureId The structure this kidnapper belongs to
     */
    public void setAssociatedStructure(@Nullable UUID structureId) {
        // If being newly associated with a camp, randomly assign hunter role
        if (structureId != null && this.associatedStructure == null) {
            this.isHunter = host.getRandom().nextBoolean(); // 50% chance
            TiedUpMod.LOGGER.debug(
                "[KidnapperCampManager] {} assigned to camp, isHunter: {}",
                host.getNpcName(),
                this.isHunter
            );
        }
        this.associatedStructure = structureId;
    }

    // HUNTER ROLE

    /**
     * Check if this kidnapper is a "hunter" that patrols far from camp.
     *
     * @return true if this kidnapper hunts in the wilderness
     */
    public boolean isHunter() {
        return this.isHunter;
    }

    /**
     * Set whether this kidnapper is a hunter.
     *
     * @param hunter true to make this kidnapper a hunter
     */
    public void setHunter(boolean hunter) {
        this.isHunter = hunter;
    }

    // NBT SERIALIZATION

    /**
     * Save camp data to NBT.
     */
    public void saveToNBT(net.minecraft.nbt.CompoundTag tag) {
        if (this.associatedStructure != null) {
            tag.putUUID("AssociatedStructure", this.associatedStructure);
        }
        tag.putBoolean("IsHunter", this.isHunter);
    }

    /**
     * Load camp data from NBT.
     */
    public void loadFromNBT(net.minecraft.nbt.CompoundTag tag) {
        if (tag.contains("AssociatedStructure")) {
            this.associatedStructure = tag.getUUID("AssociatedStructure");
        }
        if (tag.contains("IsHunter")) {
            this.isHunter = tag.getBoolean("IsHunter");
        }
    }
}
