class ReportPolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      if user.admin? || user.moderator? || user.staff?
        scope.all
      else
        scope.where(reporter: user)
      end
    end
  end

  def index?
    Flipper.enabled?(:content_moderation, user)
  end

  def show?
    return true if user.admin? || user.moderator? || user.staff?
    return true if record.reporter == user

    CommentPolicy.new(user, record.reportable).maintainer? if record.reportable_type == 'Comment'
  end

  # rubocop:disable Metrics/CyclomaticComplexity
  def create?
    return false unless Flipper.enabled?(:content_moderation, user)

    # We don't want reports twice...
    return false if user.submitted_reports.where(reportable: record.reportable).any?

    # We don't want reports for things you can change yourself nor for comment reports
    case record.reportable_type
    when 'Package'
      !PackagePolicy.new(user, record.reportable).update?
    when 'Project'
      !ProjectPolicy.new(user, record.reportable).update?
    when 'Comment'
      !CommentPolicy.new(user, record.reportable).update? &&
        !record.reportable.commentable.is_a?(Report)
    when 'User'
      !UserPolicy.new(user, record.reportable).update?
    when 'BsRequest'
      record.reportable.creator != user.login
    end
  end
  # rubocop:enable Metrics/CyclomaticComplexity

  def notify?
    return false unless Flipper.enabled?(:content_moderation, user)

    user.moderator? || user.admin? || user.staff?
  end

  def destroy?
    return false unless Flipper.enabled?(:content_moderation, user)

    user.admin? || user.moderator? || user.staff? || record.reporter == user
  end

  def update?
    destroy?
  end
end
