module Sessions
  class Start
    def initialize(session)
      @session = session
    end

    def call
      @session.match.team.entitlements.assert_concurrent_stream!(excluding_session: @session)
      @session.begin_connect! if @session.may_begin_connect?
      @session.update!(status: "connecting") unless @session.connecting?
      broadcast_status("connecting")
      @session
    end

    private

    def broadcast_status(status)
      SessionChannel.broadcast_message(@session, { type: "stream_event", event: "starting", status: status })
    end
  end
end
