package com.tiedup.remake.compat.mca.capability;

import com.tiedup.remake.compat.mca.MCACompat;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable;
import net.minecraftforge.common.util.LazyOptional;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Capability provider for MCA villager bondage state.
 *
 * Attaches MCAKidnappedCapability to MCA villager entities.
 * Handles NBT serialization for persistence.
 */
public class MCAKidnappedProvider
    implements ICapabilitySerializable<CompoundTag>
{

    private final MCAKidnappedCapability capability;
    private final LazyOptional<MCAKidnappedCapability> lazyOptional;

    public MCAKidnappedProvider() {
        this.capability = new MCAKidnappedCapability();
        this.lazyOptional = LazyOptional.of(() -> capability);
    }

    @Override
    public @NotNull <T> LazyOptional<T> getCapability(
        @NotNull Capability<T> cap,
        @Nullable Direction side
    ) {
        if (cap == MCACompat.MCA_KIDNAPPED) {
            return lazyOptional.cast();
        }
        return LazyOptional.empty();
    }

    @Override
    public CompoundTag serializeNBT() {
        return capability.serializeNBT();
    }

    @Override
    public void deserializeNBT(CompoundTag tag) {
        capability.deserializeNBT(tag);
    }

    /**
     * Invalidate the LazyOptional when this provider is no longer valid.
     * Should be called when the entity is removed.
     */
    public void invalidate() {
        lazyOptional.invalidate();
    }
}
