class ContractTemplatePolicy < ApplicationPolicy
  class Scope < Scope
    def resolve
      if user.account_manager?
        scope.left_outer_joins(:project).where(projects: {account: user.account})
      else
        scope.left_outer_joins(project: :project_memberships).where(project_memberships: { user_id: user.id })
      end
    end
  end

  def create?
    user.manager? || user.account_manager?
  end

  def show?
    true
  end

  def destroy?
    create?
  end

  def import?
    if user.account_manager?
      record.project.account = user.account
    elsif user.manager?
      user.project_memberships.exists?(project: record.project)
    else
      false
    end
  end

  private

  def has_signed_releases?
    record.present? && !record.releases.size.zero?
  end
end
