module Travis::API::V3
  class Queries::Branches < Query
    params :exists_on_github, prefix: :branch
    params :name, prefix: :branch

    sortable_by :name,
      last_build:       "builds.id".freeze,
      exists_on_github: sort_condition(:exists_on_github),
      default_branch:   sort_condition(name: "repositories.default_branch")

    default_sort "default_branch,exists_on_github,last_build:desc"

    def find(repository)
      sort(filter(repository.branches), repository: repository)
    end

    def sort_by(collection, field, repository: nil, **options)
      return super unless field == "default_branch".freeze

      if repository
        options[:sql] = sort_condition(name: quote(repository.default_branch_name))
      else
        collection    = collection.joins(:repository)
      end

      super(collection, field, **options)
    end

    def filter(list)
      list = list.where(exists_on_github: bool(exists_on_github)) if exists_on_github.present?
      list = list.where(['lower(name)  LIKE ?', "%#{name.downcase}%"]) if name.present?

      list
    end
  end
end
