package role

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

type deleteOpts struct {
	RoleID string `src:"path@role_id" desc:"角色ID"` // 角色 ID
}

var _ = Router.Delete("/{role_id}", "删除角色", deleteRole)

type deleteRoleResponse struct {
	Message string `json:"message"`
	ID      string `json:"id"`
}

func deleteRole(x *vigo.X, opts *deleteOpts) (*deleteRoleResponse, error) {
	// 查找角色
	role := &models.Role{}
	if err := cfg.DB().Where("id = ?", opts.RoleID).First(role).Error; err != nil {
		return nil, vigo.NewError("role not found").WithCode(404)
	}

	// 执行删除操作
	if err := cfg.DB().Delete(role).Error; err != nil {
		return nil, err
	}

	// 返回成功消息
	return &deleteRoleResponse{
		Message: "role deleted successfully",
		ID:      opts.RoleID,
	}, nil
}
