# frozen_string_literal: true

module Lite
  module Regulation
    module Expiration

      extend ActiveSupport::Concern

      # rubocop:disable Style/Lambda
      included do
        scope :expired, -> do
          where('expires_at IS NULL OR expires_at < ?', Lite::Regulation::Base.timestamp)
        end
        scope :unexpired, -> do
          where('expires_at IS NOT NULL AND expires_at >= ?', Lite::Regulation::Base.timestamp)
        end
      end
      # rubocop:enable Style/Lambda

      def expire!
        return true if expires_at.nil?

        update(expires_at: nil)
      end

      def expired?
        return true if expires_at.nil?

        Lite::Regulation::Base.timestamp >= expires_at
      end

      def extend!(amount = nil)
        update(expires_at: extension_date(amount))
      end

      def unexpire!
        return true unless expires_at.nil?

        update(expires_at: extension_date)
      end

      def unexpired?
        return false if expires_at.nil?

        Lite::Regulation::Base.timestamp < expires_at
      end

      def expires_at_or_time(amount = nil)
        return expires_at if unexpired?

        extension_date(amount)
      end

      def to_expiration
        I18n.t("lite.regulation.expiration.#{:un if unexpired?}expired")
      end

      private

      def extension_date(amount = nil)
        amount = 30 if amount.nil?
        return amount unless amount.is_a?(Integer)

        Lite::Regulation::Base.timestamp + amount
      end

    end
  end
end