package moe.oko.opennaw.util;

import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.group.Group;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node;
import net.luckperms.api.node.NodeType;
import net.luckperms.api.node.types.InheritanceNode;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

public record GroupHandler(LuckPerms luckPerms) {

    public void addPlayerToGroup(OfflinePlayer player, Group group) {
        this.luckPerms.getUserManager().modifyUser(player.getUniqueId(), (User user) -> {

            // Remove all other inherited groups the user had before.
            user.data().clear(NodeType.INHERITANCE::matches);

            // Create a node to add to the player.
            Node node = InheritanceNode.builder(group).build();

            // Add the node to the user.
            user.data().add(node);
        });

    }

    public void removePlayerFromGroup(OfflinePlayer player) {
        this.luckPerms.getUserManager().modifyUser(player.getUniqueId(), (User user) -> {
            user.data().clear(NodeType.INHERITANCE::matches);
        });
    }

    public Group getGroupFromString(String s) {
        return this.luckPerms.getGroupManager().getGroup(s);
    }
    public String getPrefixFromPlayer(Player player) {
        var user = this.luckPerms.getUserManager().getUser(player.getUniqueId());
        return user.getCachedData().getMetaData().getPrefix();
    }
}
