package com.tiedup.remake.commands.subcommands;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.tiedup.remake.cells.ConfiscatedInventoryRegistry;
import com.tiedup.remake.commands.CommandHelper;
import com.tiedup.remake.core.SystemMessageManager;
import net.minecraft.ChatFormatting;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;

/**
 * Inventory management sub-commands for /tiedup.
 * Handles: returnstuff
 */
@SuppressWarnings("null")
public class InventorySubCommand {

    public static void register(
        LiteralArgumentBuilder<CommandSourceStack> root
    ) {
        // /tiedup returnstuff <player>
        root.then(
            Commands.literal("returnstuff")
                .requires(CommandHelper.REQUIRES_OP)
                .then(
                    Commands.argument(
                        "player",
                        EntityArgument.player()
                    ).executes(InventorySubCommand::returnstuff)
                )
        );
    }

    // Command Implementations

    /**
     * /tiedup returnstuff <player>
     *
     * Restore a player's confiscated inventory from the NBT backup.
     * Items are given directly to the player, bypassing the chest.
     */
    private static int returnstuff(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer targetPlayer = EntityArgument.getPlayer(context, "player");
        ServerLevel level = context.getSource().getLevel();

        ConfiscatedInventoryRegistry registry =
            ConfiscatedInventoryRegistry.get(level);

        if (!registry.hasConfiscatedInventory(targetPlayer.getUUID())) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable(
                        "command.tiedup.inventory.no_confiscated", targetPlayer.getName().getString()
                    )
                );
            return 0;
        }

        boolean restored = registry.restoreInventory(targetPlayer);

        if (restored) {
            context
                .getSource()
                .sendSuccess(
                    () ->
                        Component.translatable(
                            "command.tiedup.inventory.restored", targetPlayer.getName().getString()
                        ).withStyle(ChatFormatting.GREEN),
                    true
                );
            SystemMessageManager.sendToPlayer(
                targetPlayer,
                SystemMessageManager.MessageCategory.INFO,
                "Your confiscated items have been returned!"
            );
            return 1;
        }

        context
            .getSource()
            .sendFailure(
                Component.translatable(
                    "command.tiedup.inventory.restore_failed", targetPlayer.getName().getString()
                )
            );
        return 0;
    }
}
