package ii

import (
	"fmt"
	"regexp"
	"strings"

	"git.simanc.com/software/golib/v4/features/mo"
)

func (c *ItemInfo) init() error {
	c.fieldMap = make(map[string]int)
	c.uniqueMap = make(map[string]int)

	for idx, field := range c.Fields {
		if field.Unique {
			c.uniqueMap[field.Name] = idx
		}
		if f, ok := internalField[field.Name]; ok {
			if field.Type != f.Type { // 内部字段被指定时需要使用相同的数据类型
				return fmt.Errorf("internal field type mismatch: %s.%s: %s->%s", c.Name, field.Name, f.Type.String(), field.Type.String())
			}
		}
	}

	fields := make([]*FieldInfo, len(c.Fields))
	for i := range c.Fields {
		fields[i] = &c.Fields[i]
	}
	if err := initField(c.Name.ItemName(), fields); err != nil {
		return err
	}
	return initFieldMap(c.Name.ItemName(), fields, c.fieldMap)
}

func initField(prefix string, fields []*FieldInfo) error {
	if err := initEnums(prefix, fields); err != nil {
		return err
	}
	if err := initValue(prefix, fields); err != nil {
		return err
	}
	if err := initPattern(prefix, fields); err != nil {
		return err
	}
	initSubFieldMap(fields)
	return nil
}

// initFieldMap 创建字段索引
func initFieldMap(prefix string, fields []*FieldInfo, fieldMap map[string]int) error {
	for i, field := range fields {
		if field.fieldMap == nil {
			field.fieldMap = make(map[string]int)
		}
		if field.Type == mo.TypeUndefined {
			return fmt.Errorf("%s: undefined type: %s", prefix, field.Name)
		}
		if !isEnabledType(field.Type) {
			return fmt.Errorf("%s: unenabled type: %s -> %s", prefix, field.Type.String(), field.Name)
		}
		if field.Type == mo.TypeObject || (field.Type == mo.TypeArray && field.Items == FieldItemsObject) {
			if !field.NoField {
				if len(field.Fields) == 0 {
					return fmt.Errorf("%s: %s: object type undefined sub field", prefix, field.Name)
				}
				pref := strings.Join([]string{prefix, field.Name}, ".")
				if err := initField(pref, field.getFields()); err != nil {
					return err
				}
				if err := initFieldMap(pref, field.getFields(), field.fieldMap); err != nil {
					return err
				}
			}
		}
		for _, l := range field.Lookup {
			if l.ForeignField == "" || l.From == "" || l.AS == "" {
				return fmt.Errorf("%s: %s.Lookup: config error", prefix, field.Name)
			}
			if l.AS == field.Name {
				return fmt.Errorf("%s: duplicate names are not allowed: Field.Name: %s, Lookup.AS: %s", prefix, field.Name, l.AS)
			}
		}
		if len(field.Lookup) > 0 && len(field.Fields) == 0 {
			return fmt.Errorf("%s: %s.Lookup: must be set Field in Fields, because has Lookup", prefix, field.Name)
		}
		for _, s := range field.Set {
			if s.Name == "" || s.OP == "" || s.Value == "" {
				return fmt.Errorf("%s: %s.Set: config error", prefix, field.Name)
			}
		}
		fieldMap[field.Name] = i
	}
	return nil
}

// initEnums 初始化枚举类型值
func initEnums(prefix string, fields []*FieldInfo) error {
	for _, field := range fields {
		length := len(field.Enums)
		enum := make([]any, length)
		for j := 0; j < length; j++ {
			val, err := field.Convert(field.Enums[j])
			if err != nil {
				return fmt.Errorf("%s.%s: initEnums: %s", prefix, field.Name, err)
			}
			enum[j] = val
		}
		field.enums = enum
	}
	return nil
}

// initValue 初始化默认值类型
func initValue(prefix string, fields []*FieldInfo) error {
	for _, field := range fields {
		field.defaultValue = field.Type.Default() // 先使用默认类型初始化
		if field.Default == "" {
			continue
		}
		val, err := field.ConvertWithValidate(field.Default)
		if err != nil {
			return fmt.Errorf("%s.%s: initValue: %s", prefix, field.Name, err)
		}
		field.defaultValue = val
	}
	return nil
}

func initPattern(prefix string, fields []*FieldInfo) error {
	for _, field := range fields {
		if field.Pattern != "" {
			regex, err := regexp.Compile(field.Pattern)
			if err != nil {
				return fmt.Errorf("%s.%s: initPattern: %s", prefix, field.Name, err)
			}
			field.pattern = regex
		}
	}
	return nil
}

// initMap 初始化必填和唯一
func initSubFieldMap(fields []*FieldInfo) {
	for idx, field := range fields {
		if field.uniqueMap == nil {
			field.uniqueMap = make(map[string]int)
		}
		for _, sub := range field.Fields {
			if sub.Unique {
				field.uniqueMap[field.Name+"."+sub.Name] = idx
			}
		}
	}
}
