class Recording < ApplicationRecord
  STATUSES = %w[processing ready expired].freeze

  belongs_to :stream_session
  belongs_to :team

  validates :status, inclusion: { in: STATUSES }

  scope :ready, -> { where(status: "ready").where("expires_at IS NULL OR expires_at > ?", Time.current) }
  scope :for_team, ->(team) { where(team: team) }

  def replay_url
    return nil unless ready? && stream_session_id.present?

    "#{MatchLiveTv.app_public_url.chomp('/')}/replay/#{stream_session_id}"
  end

  def ready?
    status == "ready" && (expires_at.nil? || expires_at.future?)
  end
end
