#require "workers/delete_expired_server_worker"

module Devops
  module Executor
    class ExpirationScheduler
      def initialize(expires, server)
        @expires, @server = expires, server
      end

      def schedule_expiration!
        return unless @expires
        DeleteExpiredServerWorker.perform_in(interval_in_seconds, server_id: @server.id)
      end

      def interval_in_seconds
        interval = @expires.to_i
        measure_unit = @expires.chars.last
        case measure_unit
        when 's'
          interval
        when 'm'
          interval * 60
        when 'h'
          interval * 60 * 60
        when 'd'
          interval * 60 * 60 * 24
        when 'w'
          interval * 60 * 60 * 24 * 7
        else
          raise 'Wrong interval format'
        end
      end
    end
  end
end
