class Clacky::Channel::Adapters::Feishu::WSClient

def fetch_ws_endpoint

def fetch_ws_endpoint
  uri = URI.parse("#{@domain}/callback/ws/endpoint")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  request = Net::HTTP::Post.new(uri.path)
  request["Content-Type"] = "application/json"
  request["locale"] = "en"
  request.body = JSON.generate({ AppID: @app_id, AppSecret: @app_secret })
  response = http.request(request)
  data = JSON.parse(response.body)
  if data["code"] != 0
    Clacky::Logger.warn("[feishu-ws] Failed to get endpoint: code=#{data["code"]} msg=#{data["msg"]}")
    raise "Failed to get WebSocket endpoint: #{data['msg']}"
  end
  client_config = data.dig("data", "ClientConfig") || {}
  @ping_interval = (client_config["PingInterval"] || 90).to_i
  url = data.dig("data", "URL")
  if url.nil? || url.strip.empty?
    Clacky::Logger.error("[feishu-ws] WebSocket endpoint URL is missing from response. " \
                         "Please verify your Feishu App ID and App Secret are correct.")
    raise "Failed to get WebSocket endpoint: URL is missing (check your Feishu App ID / App Secret)"
  end
  if url =~ /service_id=(\d+)/
    @service_id = $1.to_i
  end
  url
end