import gleam/dynamic/decode
import gleam/json

pub type NewUserState {
  NewUserState
}

pub type NewUserClientMessages {
  JoinAsPlayer(token: String, user_id: Int, game_code: String)
  JoinAsHost(token: String, user_id: Int, game_code: String)
}

pub fn new_user_client_messages_decoder() -> decode.Decoder(
  NewUserClientMessages,
) {
  use variant <- decode.field("type", decode.string)
  case variant {
    "JoinAsPlayer" -> {
      use token <- decode.field("token", decode.string)
      use user_id <- decode.field("user_id", decode.int)
      use game_code <- decode.field("game_code", decode.string)
      decode.success(JoinAsPlayer(token:, user_id:, game_code:))
    }
    "JoinAsHost" -> {
      use token <- decode.field("token", decode.string)
      use user_id <- decode.field("user_id", decode.int)
      use game_code <- decode.field("game_code", decode.string)
      decode.success(JoinAsHost(token:, user_id:, game_code:))
    }
    _ -> decode.failure(JoinAsPlayer("", 0, ""), "NewUserClientMessages")
  }
}

pub type NewUserServerMessages {
  RegisterResponse
  HostRegisterResponse
}

pub fn encode_new_user_server_messages(
  new_user_server_messages: NewUserServerMessages,
) -> json.Json {
  let msg_type = case new_user_server_messages {
    RegisterResponse -> json.string("RegisterResponse")
    HostRegisterResponse -> json.string("HostRegisterResponse")
  }

  json.object([#("type", msg_type)])
}
