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

package providers

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

// DeleteRequest 删除请求
type DeleteRequest struct {
	Code string `src:"path@code" desc:"提供商代码"`
}

// del 删除 OAuth 提供商
func del(x *vigo.X, req *DeleteRequest) error {
	db := cfg.DB()

	// 查找现有配置
	var provider models.OAuthProvider
	if err := db.Where("code = ?", req.Code).First(&provider).Error; err != nil {
		return vigo.ErrNotFound.WithError(err)
	}

	// 内置的不能删除
	if provider.IsBuiltIn {
		return vigo.ErrForbidden.WithString("cannot delete built-in provider, you can disable it")
	}

	// 删除
	if err := db.Where("code = ?", req.Code).Delete(&models.OAuthProvider{}).Error; err != nil {
		return vigo.ErrInternalServer.WithError(err)
	}

	return nil
}
