class Admins::UpdateUserService < ApplicationService
  Error = Class.new(StandardError)

  attr_reader :user, :params

  def initialize(user, params)
    @user   = user
    @params = params
  end

  def call
    
    user.assign_attributes(user_attributes)
    user.mail = params[:mail].to_s.presence
    user.phone = params[:phone].to_s.presence
    user.firstname = ''
    user.password = params[:password] if params[:password].present?

    if params[:identity].to_s == 'student'
      params[:technical_title] = nil
    else
      params[:student_id] = nil
    end
    user.user_extension.assign_attributes(user_extension_attributes)

    ActiveRecord::Base.transaction do
      if user.authentication_changed? && user.authentication
        RewardGradeService.call(user, container_id: user.id, container_type: 'Authentication', score: 500)
      end
      if user.professional_certification_changed? && user.professional_certification
        RewardGradeService.call(user, container_id: user.id, container_type: 'Professional', score: 500)
      end
      user.save!
      user.user_extension.save!
      user.update!(is_shixun_marker: true) if user.is_certification_teacher

      

      update_gitlab_password if params[:password].present?
    end

    user
  end

  private

  def user_attributes
    allow_attributes = %i[lastname nickname mail phone business is_test 
                          professional_certification authentication is_shixun_marker admin_visitable]

    allow_attributes.push(:admin) if user.admin?
    params.slice(*allow_attributes)
  end

  def user_extension_attributes
    params.slice(*%i[gender identity technical_title student_id location location_city school_id department_id])
  end

  def update_gitlab_password
    return if user.gid.blank?
    # 同步修改gitlab密码
    Gitlab.client.edit_user(user.gid, password: params[:password])
  rescue Exception => ex
    Util.logger_error(ex)
    raise Error, '保存失败'
  end
end