module Streams # Catena effective cover: partita → squadra → società → default Match Live TV. class CoverResolver Result = Struct.new(:record, :source, :cover_url, :slate_local_path, keyword_init: true) DEFAULT_COVER_URL = "/images/copertina-canale.png".freeze def initialize(match) @match = match @team = match.team @club = @team.club @entitlements = @team.entitlements end def effective_cover_url resolve&.cover_url || default_cover_url end def cover_source resolve&.source || "default" end def resolve return nil unless @entitlements.can_use_custom_cover? candidates.each do |record, source| next unless record.cover_image.attached? next unless record.cover_slate.attached? path = CoverSlatePaths.local_path_for(record) next if path.blank? return Result.new( record: record, source: source, cover_url: blob_path(record.cover_image), slate_local_path: path ) end nil end def default_cover_url DEFAULT_COVER_URL end def default_slate_path ENV.fetch("MEDIAMTX_SLATE_FILE", "/slates/offline.mp4") end private def candidates [ [@match, "match"], [@team, "team"], [@club, "club"] ] end def blob_path(attachment) Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true) end end end