import app/web
import gleam/dict
import gleam/erlang/process
import player_session

pub type PlayerManager {
  PlayerManager(subject: process.Subject(Request))
}

pub type Message {
  AddPlayer(username: String, game_code: String)
  AttachSocketToPlayer(user_id: Int, socket_id: Int)
  RemoveSocket(user_id: Int)
  RemovePlayer(user_id: Int)
  BroadcastMessageToPlayer(user_id: Int, msg: String)
  BroadcastMessageToPlayers(user_id: List(Int), msg: String)
}

pub type PlayerError {
  PlayerNotFound(Int)
  UsernameAlreadyExists(String)
}

pub type Return {
  Ack
  AddPlayerResp(user_id: Int, token: String)
}

pub type Response =
  Result(Return, PlayerError)

pub type Request {
  Request(reply_wth: process.Subject(Response), message: Message)
}

type State {
  State(ctx: web.Context, players: dict.Dict(Int, player_session.PlayerSession))
}
