# frozen_string_literal: true

class EbookOperation < ApplicationPolicy
  include AbilityHelpers
  include Skylight::Helpers

  protected

    alias_attribute :ebook, :target

    instrument_method
    def accessible_online?
      ebook.published? && !ebook.tombstone?
    end

    instrument_method
    def accessible_offline?
      ebook.allow_download? && accessible_online?
    end

    instrument_method
    def unrestricted?
      ebook.open_access? || !ebook.restricted?
    end

    instrument_method
    def licensed_for?(entitlement) # rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
      licenses = authority.licenses_for(actor, ebook)

      return true if licenses
                      .where(licensee_type: "Greensub::Individual")
                      .any? { |license| license.allows?(entitlement) }

      return true if licenses
                      .where(licensee_type: "Greensub::Institution")
                      .any? { |license| license.allows?(entitlement) && (license.affiliations.map(&:affiliation) & actor.affiliations(license.licensee).map(&:affiliation)).any? }

      world_institution_ids = Greensub::Institution.where(identifier: Settings.world_institution_identifier).pluck(:id).to_a
      return true if licenses
                       .where(licensee_type: "Greensub::Institution", licensee_id: world_institution_ids)
                       .any? { |license| license.allows?(entitlement) }

      false
    end
end
