class Admins::UsersController < Admins::BaseController
  before_action :require_admin
  before_action :finder_user, except: [:index] 

  def index
    params[:sort_by] = params[:sort_by].presence || 'created_on'
    params[:sort_direction] = params[:sort_direction].presence || 'desc'

    users = Admins::UserQuery.call(params)
    @users = paginate users.includes(:user_extension, projects: :members)
  end

  def edit
  end

  def update
    Admins::UpdateUserService.call(@user, update_params)
    flash[:success] = '保存成功'
    redirect_to edit_admins_user_path(@user)
  rescue ActiveRecord::RecordInvalid
    flash.now[:danger] = '保存失败'
    render 'edit'
  rescue Admins::UpdateUserService::Error => ex
    flash.now[:danger] = ex.message
    render 'edit'
  end

  def destroy
    UserAction.create(action_id: @user.id, action_type: "DestroyUser", user_id: current_user.id, :ip => request.remote_ip, data_bank: @user.attributes.to_json)
    # org_ids = TeamUser.where(user_id: @user.id).pluck(:organization_id) | OrganizationUser.where(user_id: @user.id).pluck(:organization_id)
    # organizations = Organization.where(id: org_ids)
    # organizations.each do |org|
    #   # org.team_users.joins(:team).where(user_id: @user.id, teams: {authorize: %w(owner)})
    #   owner_count = org.team_users.joins(:team).where(teams: {authorize: %w(owner)}).count
    #   # 多个owner时,仅将用户从组织移除, 一个时直接删除
    #   if owner_count > 1
    #     org.team_users.joins(:team).where(user_id: @user.id, teams: {authorize: %w(owner)}).destroy_all
    #     org.organization_users.where(user_id: @user.id, organization_id: org.id).destroy_all
    #   else
    #     org.destroy
    #   end
    # end
    # @user.destroy!
    # Gitea::User::DeleteService.call(@user.login, true)
    #
    # render_delete_success

    @result_object = Api::V1::Users::DeleteUserService.call(@user)
    if @result_object
      render_delete_success
    else
      render_js_error('删除失败!')
    end
  end

  def lock
    @user.lock!
    UserAction.create(action_id: @user.id, action_type: "LockUser", user_id: current_user.id, :ip => request.remote_ip)
    render_ok
  end

  def unlock
    @user.activate!

    render_ok
  end

  def reward_grade
    return render_unprocessable_entity('金币数量必须大于0') if params[:grade].to_i <= 0

    RewardGradeService.call(@user, container_id: @user.id, container_type: 'Feedback', score: params[:grade].to_i, not_unique: true)

    render_ok(grade: @user.grade)
  end

  def reset_login_times
    @user.reset_login_times!

    render_ok
  end


  def fresh_gitea_token
    @user.fresh_gitea_token
    render_ok
  end

  private

  def finder_user
    @user = User.find(params[:id])
  end

  def update_params
    params.require(:user).permit(%i[lastname nickname gender technical_title is_shixun_marker
                                    mail phone location location_city school_id department_id admin
                                    password  login website_permission business glcc_admin])
  end
end
