package com.tiedup.remake.v2.bondage;

import com.tiedup.remake.v2.BodyRegionV2;
import java.util.Map;
import java.util.UUID;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraftforge.common.capabilities.AutoRegisterCapability;
import org.jetbrains.annotations.Nullable;

/**
 * Capability interface for V2 bondage equipment storage.
 *
 * Stores ItemStacks keyed by {@link BodyRegionV2}. Multi-region items
 * share the same ItemStack reference across all occupied regions.
 *
 * CRITICAL: The {@link AutoRegisterCapability} annotation is required
 * for Forge to register this capability automatically.
 */
@AutoRegisterCapability
public interface IV2BondageEquipment {
    /**
     * Get the item equipped in the given region.
     * @return The ItemStack in that region, or {@link ItemStack#EMPTY} — never null.
     */
    ItemStack getInRegion(BodyRegionV2 region);

    /**
     * Set the item in a specific region. Pass {@link ItemStack#EMPTY} to clear.
     */
    void setInRegion(BodyRegionV2 region, ItemStack stack);

    /**
     * Get all equipped items, de-duplicated. Multi-region items appear once.
     * @return Unmodifiable map from one representative region to the ItemStack.
     */
    Map<BodyRegionV2, ItemStack> getAllEquipped();

    /**
     * Check if a region has an item directly equipped in it.
     */
    boolean isRegionOccupied(BodyRegionV2 region);

    /**
     * Check if a region is blocked by any equipped item's {@link IV2BondageItem#getBlockedRegions()}.
     * For example, if a Hood (blocks EYES/EARS/MOUTH) is equipped, EYES is blocked.
     */
    boolean isRegionBlocked(BodyRegionV2 region);

    /**
     * Count distinct equipped items (de-duplicated for multi-region items).
     */
    int getEquippedCount();

    /**
     * Clear all regions, removing all equipped items.
     */
    void clearAll();

    /**
     * Serialize all equipped items to NBT.
     */
    CompoundTag serializeNBT();

    /**
     * Deserialize equipped items from NBT, replacing current state.
     */
    void deserializeNBT(CompoundTag tag);

    // Pole leash persistence

    /**
     * Whether the player was leashed to a pole when they disconnected.
     */
    boolean wasLeashedToPole();

    /**
     * Get the saved pole position, or null if none.
     */
    @Nullable
    BlockPos getSavedPolePosition();

    /**
     * Get the saved pole dimension, or null if none.
     */
    @Nullable
    ResourceKey<Level> getSavedPoleDimension();

    /**
     * Save the pole leash state for restoration on reconnect.
     */
    void savePoleLeash(BlockPos pos, ResourceKey<Level> dimension);

    /**
     * Clear saved pole leash state.
     */
    void clearSavedPoleLeash();

    // Captor persistence

    /**
     * Whether the player had a saved captor when they disconnected.
     */
    boolean hasSavedCaptor();

    /**
     * Get the saved captor UUID, or null if none.
     */
    @Nullable
    UUID getSavedCaptorUUID();

    /**
     * Save the captor UUID for restoration on reconnect.
     */
    void saveCaptorUUID(UUID uuid);

    /**
     * Clear saved captor state.
     */
    void clearSavedCaptor();
}
