class TheatrePostAdvancer
  def self.call(theatre:)
    new(theatre:).call
  end

  def initialize(theatre:)
    @theatre = theatre
  end

  def call
    previous_post = theatre.current_post
    post = TheatrePostSelector.new(theatre:).select

    TheatreSkipVote.where(theatre:, post: previous_post).delete_all if previous_post

    theatre.update!(current_post: post, current_post_started_at: post ? Time.current : nil)

    if post
      position = (theatre.programmes.maximum(:position) || 0) + 1
      theatre.programmes.create!(position:, post:)
    end

    post
  end

  private

  attr_reader :theatre
end
