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

package models

import "github.com/veypi/vbase/cfg"

// OAuthProviderTemplates 内置 OAuth 提供商模板
// 首次启动时自动创建到数据库
var OAuthProviderTemplates = []OAuthProvider{
	{
		Code:           "google",
		Name:           "Google",
		Icon:           "google",
		Enabled:        false,
		AuthURL:        "https://accounts.google.com/o/oauth2/v2/auth",
		TokenURL:       "https://oauth2.googleapis.com/token",
		UserInfoURL:    "https://openidconnect.googleapis.com/v1/userinfo",
		Scopes:         []string{"openid", "email", "profile"},
		UserIDPath:     "sub",
		UserNamePath:   "name",
		UserEmailPath:  "email",
		UserAvatarPath: "picture",
		IsBuiltIn:      true,
	},
	{
		Code:           "github",
		Name:           "GitHub",
		Icon:           "github",
		Enabled:        false,
		AuthURL:        "https://github.com/login/oauth/authorize",
		TokenURL:       "https://github.com/login/oauth/access_token",
		UserInfoURL:    "https://api.github.com/user",
		Scopes:         []string{"user:email", "read:user"},
		UserIDPath:     "id",
		UserNamePath:   "name",
		UserEmailPath:  "email",
		UserAvatarPath: "avatar_url",
		ExtraConfig:    map[string]string{"use_post_token": "true", "token_in_body": "true"},
		IsBuiltIn:      true,
	},
	{
		Code:           "wechat",
		Name:           "微信",
		Icon:           "wechat",
		Enabled:        false,
		AuthURL:        "https://open.weixin.qq.com/connect/qrconnect",
		TokenURL:       "https://api.weixin.qq.com/sns/oauth2/access_token",
		UserInfoURL:    "https://api.weixin.qq.com/sns/userinfo",
		Scopes:         []string{"snsapi_login"},
		UserIDPath:     "unionid",
		UserNamePath:   "nickname",
		UserAvatarPath: "headimgurl",
		ExtraConfig:    map[string]string{"appid_param": "appid"},
		IsBuiltIn:      true,
	},
	// 可继续添加更多预设...
	{
		Code:           "feishu",
		Name:           "飞书",
		Icon:           "feishu",
		Enabled:        false,
		AuthURL:        "https://open.feishu.cn/open-apis/authen/v1/index",
		TokenURL:       "https://open.feishu.cn/open-apis/authen/v1/access_token",
		UserInfoURL:    "https://open.feishu.cn/open-apis/authen/v1/user_info",
		Scopes:         []string{"contact:user.employee_id:readonly"},
		UserIDPath:     "data.employee_id",
		UserNamePath:   "data.name",
		UserEmailPath:  "data.email",
		UserAvatarPath: "data.avatar_url",
		IsBuiltIn:      true,
	},
	{
		Code:           "dingtalk",
		Name:           "钉钉",
		Icon:           "dingtalk",
		Enabled:        false,
		AuthURL:        "https://login.dingtalk.com/oauth2/auth",
		TokenURL:       "https://api.dingtalk.com/v1.0/oauth2/userAccessToken",
		UserInfoURL:    "https://api.dingtalk.com/v1.0/contact/users/me",
		Scopes:         []string{"openid", "corpid"},
		UserIDPath:     "openId",
		UserNamePath:   "nick",
		UserAvatarPath: "avatarUrl",
		IsBuiltIn:      true,
	},
}

// InitOAuthProviders 初始化内置 OAuth 提供商
func InitOAuthProviders() error {
	db := cfg.DB()
	for _, tpl := range OAuthProviderTemplates {
		var count int64
		if err := db.Model(&OAuthProvider{}).Where("code = ?", tpl.Code).Count(&count).Error; err != nil {
			return err
		}
		if count == 0 {
			if err := db.Create(&tpl).Error; err != nil {
				return err
			}
		}
	}
	return nil
}
