class MillicastDestination
  attr_reader :name, :token
  
  def self.create
    token_stream_name = SecureRandom.urlsafe_base64

    publish_token = Millicast::PublishToken.create(
      label: SecureRandom.urlsafe_base64, 
      streams: [
        { streamName: token_stream_name }
      ]
    )

    new(token_stream_name, publish_token.data.token)
  end

  def initialize(name, token)
    @name = name
    @token = token
  end

  def account_id
    ENV["MILLICAST_ACCOUNT_ID"]
  end

  def key
    "#{name}?token=#{token}"
  end

  def url
    "rtmp://live-rtmp-pub.millicast.com:1935/v2/pub"
  end

  def playback_url
    "https://viewer.millicast.com/v2?streamId=#{account_id}/#{name}"
  end

  def playback_embed
    "<iframe src=\"#{playback_url}\" allowfullscreen width=\"640\" height=\"480\"></iframe>"
  end
end
