package identity

import "testing"

func TestSettingValueBool(t *testing.T) {
	tests := []struct {
		name         string
		raw          SettingValue
		defaultValue bool
		want         bool
	}{
		{name: "json true", raw: SettingValue(`true`), want: true},
		{name: "json false", raw: SettingValue(`false`), defaultValue: true, want: false},
		{name: "json string enabled", raw: SettingValue(`"enabled"`), want: true},
		{name: "json string disabled", raw: SettingValue(`"disabled"`), defaultValue: true, want: false},
		{name: "number zero", raw: SettingValue(`0`), defaultValue: true, want: false},
		{name: "plain on", raw: SettingValue(`on`), want: true},
		{name: "empty uses default", raw: nil, defaultValue: false, want: false},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.raw.Bool(tt.defaultValue); got != tt.want {
				t.Fatalf("Bool() = %v, want %v", got, tt.want)
			}
		})
	}
}

func TestEmailPluginConfigUsesManifestFieldNames(t *testing.T) {
	config := emailPluginConfig(emailConfig{
		Host: "smtp.example.com", Port: 587, User: "mailer", Password: "secret",
		FromEmail: "no-reply@example.com", FromName: "清云互联", Security: "starttls",
		AuthEnabled: true, ValidateCerts: true, Timeout: 15,
	})
	for key, want := range map[string]any{
		"smtp_host": "smtp.example.com", "smtp_port": 587, "smtp_username": "mailer",
		"smtp_password": "secret", "smtp_from": "no-reply@example.com",
		"smtp_from_name": "清云互联", "smtp_security": "starttls",
	} {
		if got := config[key]; got != want {
			t.Fatalf("config[%q] = %#v, want %#v", key, got, want)
		}
	}
}

func TestVerificationPluginConfigUsesManifestFieldNames(t *testing.T) {
	cfg := verificationConfig{
		AliyunAppCode:       "app-code",
		AliyunEndpoint:      "https://id.example.com",
		TencentSecretID:     "secret-id",
		TencentSecretKey:    "secret-key",
		TencentWechatRuleID: "rule-id",
		TencentEndpoint:     "faceid.example.com",
		TencentInfoType:     "1",
	}

	aliyun := verificationPluginConfig(cfg, "aliyun_idcert")
	if aliyun["appcode"] != "app-code" || aliyun["endpoint"] != "https://id.example.com" {
		t.Fatalf("unexpected aliyun config: %#v", aliyun)
	}
	tencent := verificationPluginConfig(cfg, "tencent_wechat_h5")
	if tencent["secret_id"] != "secret-id" || tencent["secret_key"] != "secret-key" || tencent["wechat_h5_rule_id"] != "rule-id" {
		t.Fatalf("unexpected tencent config: %#v", tencent)
	}
}
