module Recordings
  class IndexSession
    def initialize(session)
      @session = session
    end

    def call
      team = @session.match.team
      return unless team.entitlements.can_access_recordings?

      retention_days = team.entitlements.recording_retention_days
      expires_at = retention_days.positive? ? retention_days.days.from_now : nil

      recording = Recording.find_or_initialize_by(stream_session: @session)
      recording.assign_attributes(
        team: team,
        status: "ready",
        storage_path: @session.mediamtx_path_name,
        expires_at: expires_at
      )
      recording.save!
      recording
    end
  end
end
