function formatUUID(uuidStr) {
  return `${uuidStr.slice(0, 8)}-${uuidStr.slice(8, 12)}-${uuidStr.slice(12, 16)}-${uuidStr.slice(16, 20)}-${uuidStr.slice(20)}`;
}

async function validateMinecraftUser(username) {
  try {
    const res = await fetch(`https://api.mojang.com/users/profiles/minecraft/${username}`);
    if (res.status === 204 || res.status === 404) return null;
    if (!res.ok) throw new Error(`Mojang API error: ${res.status}`);
    
    const data = await res.json();
    return { name: data.name, uuid: formatUUID(data.id) };
  } catch (error) {
    console.error("Failed to validate Minecraft user:", error);
    return null;
  }
}

module.exports = { validateMinecraftUser };
