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.firstname = ''
    user.password = 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
      user.save!
      user.user_extension.save!

      update_gitlab_password if params[:password].present?
    end

    user
  end

  private

  def user_attributes
    params.slice(*%i[lastname nickname mail phone admin business is_test
                     professional_certification authentication])
  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