package de.teleportsuite.managers;

import de.teleportsuite.TeleportSuite;
import de.teleportsuite.models.Home;
import de.teleportsuite.models.TeleportLocation;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.UUID;

public class HomeManager {
    private final TeleportSuite plugin;
    public HomeManager(TeleportSuite plugin) { this.plugin = plugin; }

    public int getMaxHomes(Player player) {
        if (player.hasPermission("teleportsuite.home.unlimited")) return Integer.MAX_VALUE;
        if (player.hasPermission("teleportsuite.home.premium")) return plugin.getConfigManager().getMaxHomes("premium");
        if (player.hasPermission("teleportsuite.home.vip")) return plugin.getConfigManager().getMaxHomes("vip");
        return plugin.getConfigManager().getMaxHomes("default");
    }

    public boolean setHome(Player player, String name) {
        int current = plugin.getDatabaseManager().countHomes(player.getUniqueId());
        int max = getMaxHomes(player);
        if (current >= max) {
            player.sendMessage(plugin.getConfigManager().getMessage("home-limit", "max", String.valueOf(max)));
            return false;
        }
        TeleportLocation loc = new TeleportLocation(player.getLocation(), plugin.getConfigManager().getServerName());
        plugin.getDatabaseManager().saveHome(player.getUniqueId(), name, loc);
        player.sendMessage(plugin.getConfigManager().getMessage("home-set", "name", name));
        return true;
    }

    public void teleportHome(Player player, String name) {
        Home home = plugin.getDatabaseManager().getHome(player.getUniqueId(), name);
        if (home == null) {
            player.sendMessage(plugin.getConfigManager().getMessage("home-not-found", "name", name));
            return;
        }
        plugin.getTeleportManager().teleport(player, home.getLocation());
    }

    public boolean deleteHome(Player player, String name) {
        boolean deleted = plugin.getDatabaseManager().deleteHome(player.getUniqueId(), name);
        if (deleted) player.sendMessage(plugin.getConfigManager().getMessage("home-deleted", "name", name));
        else player.sendMessage(plugin.getConfigManager().getMessage("home-not-found", "name", name));
        return deleted;
    }

    public List<Home> getHomes(UUID uuid) {
        return plugin.getDatabaseManager().getHomes(uuid);
    }
}
