class SessionManager {
  constructor() {
    this.sessions = new Map();
  }

  addPlayer(socketId, playerRaw) {
    this.sessions.set(socketId, {
      id: playerRaw.id,
      username: playerRaw.username,
      level: playerRaw.level,
      energy: playerRaw.energy,
      maxEnergy: playerRaw.maxEnergy,
      status: "docked",
      currentLocation: "Home_Base",
      scene: "world",
      sceneData: null,
      joinedAt: Date.now(),
      equipment: playerRaw.equipment || {},
    });
  }

  updateEquipment(socketId, slot, itemId) {
    const session = this.sessions.get(socketId);
    if (session) {
      if (itemId === null) {
        delete session.equipment[slot];
      } else {
        session.equipment[slot] = itemId;
      }
    }
  }

  setPlayerScene(socketId, sceneName, data = null) {
    const session = this.sessions.get(socketId);
    if (session) {
      session.scene = sceneName;
      session.sceneData = data;
      session.status = sceneName === "dungeon" ? "in_mission" : "active";
    }
  }

  getPlayerSession(socketId) {
    return this.sessions.get(socketId);
  }

  updateStatus(socketId, newStatus, location = null) {
    const session = this.sessions.get(socketId);
    if (session) {
      session.status = newStatus;
      if (location) session.currentLocation = location;
    }
  }

  removePlayer(socketId) {
    this.sessions.delete(socketId);
  }

  getPlayer(socketId) {
    return this.sessions.get(socketId);
  }

  getPlayerByUserId(userId) {
    return Array.from(this.sessions.values()).find((s) => s.id === userId);
  }

  getAllOnline() {
    return Array.from(this.sessions.values());
  }
}

module.exports = new SessionManager();
