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?

    user.user_extension.assign_attributes(user_extension_attributes) if user.user_extension.present?

    old_login = user.login
    ActiveRecord::Base.transaction do
      user.save!
      user.user_extension.save! if user.user_extension.present?
      update_gitea_user(old_login)
      update_gitea_user_email(user.previous_changes[:mail])
    end

    user
  end

  private

  def user_attributes
    params.slice(*%i[lastname nickname mail phone admin business glcc_admin is_test login
                     professional_certification authentication is_shixun_marker website_permission bank_certification enterprise_certification])
  end

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

  def gitea_user_params
    hash = {
      password: params[:password].to_s.presence,
      email: user.mail,
      login_name: params[:login].to_s.presence,
      admin: boolean_admin
    }.compact

    hash.delete_if {|_,v| v.to_s.strip == ''}
  end

  def boolean_admin
    admin = params[:admin].to_s.presence
    case admin
    when "0" then false
    when "1" then true
    end
  end

  def update_gitea_user(old_login)
    return if user.gitea_uid.blank?

    Gitea::User::UpdateInteractor.call(old_login, gitea_user_params)
  rescue Exception => ex
    Util.logger_error(ex)
    raise Error, '保存失败'
  end

  def update_gitea_user_email(change_options) 
    return if change_options.blank?
    return if user.gitea_uid.blank? || user.gitea_token.blank? 
    $gitea_client.delete_user_emails({body: {emails: [change_options[0]]}.to_json, query: {access_token: user.gitea_token}})
    $gitea_client.post_user_emails({body: {emails: [change_options[1]]}.to_json, query: {access_token: user.gitea_token}})
  rescue Exception => ex
    Util.logger_error(ex)
    raise Error, '保存失败'
  end
end