package hu.jgj52.pvpcore.Commands;

import hu.jgj52.pvpcore.Main;
import hu.jgj52.pvpcore.Utils.Database;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutionException;

public class DenyDuelCommand implements CommandExecutor, TabCompleter {
    private final Main plugin;

    public DenyDuelCommand(Main plugin) {
        this.plugin = plugin;
    }

    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
        if (sender instanceof Player player) {
            if (args.length != 1) {
                player.sendMessage("§cHasználat: /denyduel <játékos>");
                return true;
            }

            Database.QueryResult reqResult = null;
            try {
                reqResult = plugin.db.from("pvpcore_duel_requests").eq("player", Objects.requireNonNull(Bukkit.getPlayer(args[0])).getUniqueId()).eq("enemy", player.getUniqueId()).execute().get();
            } catch (InterruptedException | ExecutionException e) {
                throw new RuntimeException(e);
            }

            if (reqResult.isEmpty()) {
                player.sendMessage("§cNincs párbajkérelmed.");
                return true;
            }

            Player enemy = Bukkit.getPlayer(args[0]);
            if (enemy == null) {
                player.sendMessage("§cA megadott játékos nem található.");
                return true;
            }
            plugin.db.from("pvpcore_duel_requests").eq("player", Objects.requireNonNull(Bukkit.getPlayer(args[0])).getUniqueId()).eq("enemy", player.getUniqueId()).delete();
        }
        return true;
    }

    @Override
    public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String s, @NotNull String[] args) {
        return List.of();
    }
}
