//
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
//

package user

import (
	"github.com/veypi/vbase/cfg"
	"github.com/veypi/vbase/models"
	"github.com/veypi/vigo"
)

// UpdateStatusRequest 更新状态请求
type UpdateStatusRequest struct {
	UserID string `src:"path@user_id" desc:"用户ID"`
	Status int    `json:"status" src:"json" desc:"状态: 0=禁用 1=正常 2=未激活"`
}

// updateStatus 更新用户状态
func updateStatus(x *vigo.X, req *UpdateStatusRequest) (*models.User, error) {
	var user models.User
	if err := cfg.DB().First(&user, "id = ?", req.UserID).Error; err != nil {
		return nil, vigo.ErrNotFound
	}

	if err := cfg.DB().Model(&user).Update("status", req.Status).Error; err != nil {
		return nil, vigo.ErrInternalServer.WithError(err)
	}

	return &user, nil
}
