package system

import (
	"context"

	"reflect"
	"strings"

	"github.com/perfect-panel/server/initialize"
	"github.com/perfect-panel/server/internal/config"
	"github.com/perfect-panel/server/internal/model/system"
	"github.com/perfect-panel/server/pkg/logger"
	"github.com/perfect-panel/server/pkg/tool"
	"github.com/perfect-panel/server/pkg/xerr"

	"github.com/pkg/errors"
	"gorm.io/gorm"

	"github.com/perfect-panel/server/internal/svc"
	"github.com/perfect-panel/server/internal/types"
)

type UpdateRegisterConfigLogic struct {
	logger.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateRegisterConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRegisterConfigLogic {
	return &UpdateRegisterConfigLogic{
		Logger: logger.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateRegisterConfigLogic) UpdateRegisterConfig(req *types.RegisterConfig) error {
	v := reflect.ValueOf(*req)
	// Get the reflection type of the structure
	t := v.Type()
	err := l.svcCtx.SystemModel.Transaction(l.ctx, func(db *gorm.DB) error {
		var err error
		for i := 0; i < v.NumField(); i++ {
			// Get the field name
			fieldName := t.Field(i).Name
			// Get the field value to string
			fieldValue := tool.ConvertValueToString(v.Field(i))

			var existing system.System
			queryErr := db.Where("`category` = ? and `key` = ?", "register", fieldName).First(&existing).Error
			if queryErr != nil && !errors.Is(queryErr, gorm.ErrRecordNotFound) {
				return queryErr
			}
			if errors.Is(queryErr, gorm.ErrRecordNotFound) {
				fieldValue = l.defaultRegisterFieldValue(fieldName, fieldValue)
			}

			record := &system.System{
				Category: "register",
				Key:      fieldName,
			}
			assignments := map[string]interface{}{
				"value": fieldValue,
				"type":  inferRegisterSystemValueType(t.Field(i).Type.Kind()),
				"desc":  fieldName,
			}
			err = db.Where("`category` = ? and `key` = ?", "register", fieldName).
				Assign(assignments).
				FirstOrCreate(record).Error
			if err != nil {
				break
			}
		}
		if err != nil {
			return err
		}
		return l.svcCtx.Redis.Del(l.ctx, config.RegisterConfigKey, config.GlobalConfigKey).Err()
	})
	if err != nil {
		l.Errorw("[UpdateRegisterConfig] update register config error", logger.Field("error", err.Error()))
		return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseUpdateError), "update register config error: %v", err.Error())
	}
	// init system config
	initialize.Register(l.svcCtx)
	return nil
}

func inferRegisterSystemValueType(kind reflect.Kind) string {
	switch kind {
	case reflect.Bool:
		return "bool"
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return "int"
	default:
		return "string"
	}
}

func (l *UpdateRegisterConfigLogic) defaultRegisterFieldValue(fieldName, fieldValue string) string {
	switch fieldName {
	case "EnableTrialEmailWhitelist":
		return "true"
	case "TrialEmailDomainWhitelist":
		if strings.TrimSpace(fieldValue) != "" {
			return fieldValue
		}
		return l.svcCtx.Config.Register.TrialEmailDomainWhitelist
	default:
		return fieldValue
	}
}
