class BroadcastRecording < ApplicationRecord
  belongs_to :broadcast

  delegate :name, to: :broadcast, prefix: :broadcast

  validates :asset_uid, uniqueness: true

  scope :visible, -> { where(hidden: false) }

  before_save :set_title_and_description

  def download_url
    "https://stream.mux.com/#{asset_playback_uid}/#{file_name}?download=#{name}"
  end

  def playback_url
    "https://stream.mux.com/#{asset_playback_uid}/#{file_name}"
  end

  def download_file_name
    "#{broadcast_name}_Date_#{Time.now.in_time_zone(broadcast.shoot_location_time_zone).strftime("%Y-%m-%d")}_Time_#{Time.now.in_time_zone(broadcast.shoot_location_time_zone).strftime("%T")}".parameterize
  end

  def toggle_star
    toggle! :starred
  end

  def thumbnail_url(width = 300)
    "https://image.mux.com/#{asset_playback_uid}/thumbnail.jpg?width=#{width}"
  end

  private

  def set_title_and_description
    self.name ||= download_file_name
    self.description ||= "No description provided for this recording."
  end
end
