package com.tiedup.remake.commands.subcommands;

import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.tiedup.remake.commands.CommandHelper;
import com.tiedup.remake.prison.PrisonerManager;
import com.tiedup.remake.prison.RansomRecord;
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;

/**
 * Debt management sub-commands for /tiedup.
 * Handles: debt show, debt set, debt add, debt remove
 */
@SuppressWarnings("null")
public class DebtSubCommand {

    public static void register(
        LiteralArgumentBuilder<CommandSourceStack> root
    ) {
        // /tiedup debt <player> [set|add|remove <amount>]
        root.then(
            Commands.literal("debt")
                .requires(CommandHelper.REQUIRES_OP)
                .then(
                    Commands.argument("player", EntityArgument.player())
                        // /tiedup debt <player> -> show debt
                        .executes(DebtSubCommand::debtShow)
                        // /tiedup debt <player> set <amount>
                        .then(
                            Commands.literal("set").then(
                                Commands.argument(
                                    "amount",
                                    IntegerArgumentType.integer(0)
                                ).executes(DebtSubCommand::debtSet)
                            )
                        )
                        // /tiedup debt <player> add <amount>
                        .then(
                            Commands.literal("add").then(
                                Commands.argument(
                                    "amount",
                                    IntegerArgumentType.integer(0)
                                ).executes(DebtSubCommand::debtAdd)
                            )
                        )
                        // /tiedup debt <player> remove <amount>
                        .then(
                            Commands.literal("remove").then(
                                Commands.argument(
                                    "amount",
                                    IntegerArgumentType.integer(0)
                                ).executes(DebtSubCommand::debtRemove)
                            )
                        )
                )
        );
    }

    // Command Implementations

    private static int debtShow(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer target = EntityArgument.getPlayer(context, "player");
        ServerLevel level = context.getSource().getLevel();
        PrisonerManager manager = PrisonerManager.get(level);
        RansomRecord ransom = manager.getRansomRecord(target.getUUID());

        if (ransom == null) {
            context
                .getSource()
                .sendSuccess(
                    () ->
                        Component.translatable(
                            "command.tiedup.debt.no_record", target.getName().getString()
                        ),
                    false
                );
            return 1;
        }

        int total = ransom.getTotalDebt();
        int paid = ransom.getAmountPaid();
        int remaining = ransom.getRemainingDebt();

        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        "command.tiedup.debt.show",
                        target.getName().getString(),
                        total, paid, remaining
                    ).withStyle(ChatFormatting.YELLOW),
                false
            );
        return 1;
    }

    private static int debtSet(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer target = EntityArgument.getPlayer(context, "player");
        int amount = IntegerArgumentType.getInteger(context, "amount");
        ServerLevel level = context.getSource().getLevel();
        PrisonerManager manager = PrisonerManager.get(level);
        RansomRecord ransom = manager.getRansomRecord(target.getUUID());

        if (ransom == null) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable("command.tiedup.debt.no_record", target.getName().getString())
                );
            return 0;
        }

        ransom.setTotalDebt(amount);
        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        "command.tiedup.debt.set",
                        target.getName().getString(), amount
                    ).withStyle(ChatFormatting.GREEN),
                true
            );
        return 1;
    }

    private static int debtAdd(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer target = EntityArgument.getPlayer(context, "player");
        int amount = IntegerArgumentType.getInteger(context, "amount");
        ServerLevel level = context.getSource().getLevel();
        PrisonerManager manager = PrisonerManager.get(level);
        RansomRecord ransom = manager.getRansomRecord(target.getUUID());

        if (ransom == null) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable("command.tiedup.debt.no_record", target.getName().getString())
                );
            return 0;
        }

        ransom.increaseDebt(amount);
        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        "command.tiedup.debt.added",
                        amount, target.getName().getString(), ransom.getRemainingDebt()
                    ).withStyle(ChatFormatting.GREEN),
                true
            );
        return 1;
    }

    private static int debtRemove(CommandContext<CommandSourceStack> context)
        throws CommandSyntaxException {
        ServerPlayer target = EntityArgument.getPlayer(context, "player");
        int amount = IntegerArgumentType.getInteger(context, "amount");
        ServerLevel level = context.getSource().getLevel();
        PrisonerManager manager = PrisonerManager.get(level);
        RansomRecord ransom = manager.getRansomRecord(target.getUUID());

        if (ransom == null) {
            context
                .getSource()
                .sendFailure(
                    Component.translatable("command.tiedup.debt.no_record", target.getName().getString())
                );
            return 0;
        }

        ransom.addPayment(amount, null);
        boolean paid = ransom.isPaid();
        context
            .getSource()
            .sendSuccess(
                () ->
                    Component.translatable(
                        paid ? "command.tiedup.debt.removed_paid" : "command.tiedup.debt.removed",
                        amount, target.getName().getString(), ransom.getRemainingDebt()
                    ).withStyle(ChatFormatting.GREEN),
                true
            );
        return 1;
    }
}
