module Katello
  class ContentViewPackageFilter < ContentViewFilter
    CONTENT_TYPE = Rpm::CONTENT_TYPE

    has_many :package_rules, :dependent => :destroy, :foreign_key => :content_view_filter_id,
                             :class_name => "Katello::ContentViewPackageFilterRule"
    validates_lengths_from_database

    # Returns a set of Pulp/MongoDB conditions to filter out packages in the
    # repo repository that match parameters if the repository content type
    # is managed by pulp2.
    #
    # Returns a set of pulpcore hrefs (pulp_ids) if the repository content
    # type is managed by pulpcore.
    #
    # @param repo [Repository] a repository containing packages to filter
    # @return [Array] an array of hashes with MongoDB conditions
    def generate_clauses(repo)
      package_filenames = []

      self.package_rules.each do |rule|
        package_filenames.concat(query_rpms(repo, rule))
      end
      if self.original_packages
        package_filenames.concat(repo.packages_without_errata.map(&:filename))
      end

      ContentViewPackageFilter.generate_rpm_clauses(package_filenames)
    end

    def original_packages=(value)
      self[:original_packages] = value
    end

    def content_unit_pulp_ids(repo)
      package_filenames = []
      self.package_rules.each do |rule|
        package_filenames.concat(query_rpms(repo, rule))
      end
      package_filenames.concat(repo.packages_without_errata.map(&:filename)) if self.original_packages
      rpms = Rpm.in_repositories(repo)
      rpms.where(filename: package_filenames).where(:modular => false).pluck(:pulp_id).flatten.uniq
    end

    def self.generate_rpm_clauses(package_filenames = [])
      { 'filename' => { "$in" => package_filenames.sort } } unless package_filenames.empty?
    end

    def applicable_rpms
      Rpm.in_repositories(self.applicable_repos)
    end

    def query_rpms(repo, rule)
      rpms = Rpm.in_repositories(repo)
      query_rpms_from_collection(rpms, rule).pluck("#{Rpm.table_name}.filename")
    end

    def query_rpms_from_collection(collection, rule)
      query_name = rule.name.tr("*", "%")
      query = collection.non_modular.where("#{Rpm.table_name}.name ilike ?", query_name)
      if rule.architecture.present?
        query_arch = rule.architecture.tr("*", "%")
        query = query.where("#{Rpm.table_name}.arch ilike ?", query_arch)
      end
      if rule.version.present?
        query = query.search_version_equal(rule.version)
      elsif rule.min_version.present? || rule.max_version.present?
        query = query.search_version_range(rule.min_version, rule.max_version)
      end
      query.default_sort
    end
  end
end
