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

package auth

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

// LoginMethod 登录方式
type LoginMethod struct {
	Type        string   `json:"type"`
	Name        string   `json:"name"`
	Enabled     bool     `json:"enabled"`
	Fields      []string `json:"fields,omitempty"`       // 密码登录的字段
	RequireCaptcha bool  `json:"require_captcha,omitempty"`
}

// LoginMethodsResponse 登录方式响应
type LoginMethodsResponse struct {
	Methods      []LoginMethod `json:"methods"`
	Registration RegistrationConfig `json:"registration"`
}

// RegistrationConfig 注册配置
type RegistrationConfig struct {
	RequireEmail bool     `json:"require_email"`
	RequirePhone bool     `json:"require_phone"`
	Fields       []string `json:"fields"`
}

// loginMethods 获取可用登录方式
func loginMethods(x *vigo.X) (*LoginMethodsResponse, error) {
	db := cfg.DB()

	// 获取登录方式配置
	var methods []string
	if err := models.GetSettingJSON(models.SettingAuthLoginMethods, &methods); err != nil {
		// 默认仅密码登录
		methods = []string{"password"}
	}

	var passwordFields []string
	if err := models.GetSettingJSON(models.SettingAuthPasswordFields, &passwordFields); err != nil {
		// 默认仅用户名
		passwordFields = []string{"username"}
	}

	captchaEnabled, _ := models.GetSettingBool(models.SettingSecurityCaptchaEnabled)

	// 构建登录方式列表
	var loginMethods []LoginMethod
	for _, method := range methods {
		switch method {
		case "password":
			loginMethods = append(loginMethods, LoginMethod{
				Type:           "password",
				Name:           "密码登录",
				Enabled:        true,
				Fields:         passwordFields,
				RequireCaptcha: captchaEnabled,
			})
		case "email_code":
			// 检查邮箱是否启用
			emailEnabled, _ := models.GetSettingBool(models.SettingEmailEnabled)
			if emailEnabled {
				loginMethods = append(loginMethods, LoginMethod{
					Type:    "email_code",
					Name:    "邮箱验证码",
					Enabled: true,
					Fields:  []string{"email"},
				})
			}
		case "phone_code":
			// 检查短信是否启用
			smsEnabled, _ := models.GetSettingBool(models.SettingSMSEnabled)
			if smsEnabled {
				loginMethods = append(loginMethods, LoginMethod{
					Type:    "phone_code",
					Name:    "手机验证码",
					Enabled: true,
					Fields:  []string{"phone"},
				})
			}
		}
	}

	// 添加第三方登录方式
	var providers []models.OAuthProvider
	if err := db.Where("enabled = ?", true).Order("sort_order").Find(&providers).Error; err == nil {
		for _, p := range providers {
			loginMethods = append(loginMethods, LoginMethod{
				Type:    p.Code,
				Name:    p.Name,
				Enabled: true,
			})
		}
	}

	// 获取注册配置
	requireEmail, _ := models.GetSettingBool(models.SettingAuthRegRequireEmail)
	requirePhone, _ := models.GetSettingBool(models.SettingAuthRegRequirePhone)

	regFields := []string{"username", "password"}
	if requireEmail {
		regFields = append(regFields, "email")
	}
	if requirePhone {
		regFields = append(regFields, "phone")
	}

	return &LoginMethodsResponse{
		Methods: loginMethods,
		Registration: RegistrationConfig{
			RequireEmail: requireEmail,
			RequirePhone: requirePhone,
			Fields:       regFields,
		},
	}, nil
}
