package openapi3gen

import (
	"reflect"
	"strings"
	"unicode"
	"unicode/utf8"
)

// theFieldInfo contains information about JSON serialization of a field.
type theFieldInfo struct {
	HasJSONTag        bool
	TypeIsMarshaler   bool
	TypeIsUnmarshaler bool
	JSONOmitEmpty     bool
	JSONString        bool
	Index             []int
	Type              reflect.Type
	JSONName          string
}

func appendFields(fields []theFieldInfo, parentIndex []int, t reflect.Type) []theFieldInfo {
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	}
	if t.Kind() != reflect.Struct {
		return fields
	}

	// For each field
	numField := t.NumField()
iteration:
	for i := range numField {
		f := t.Field(i)
		index := make([]int, 0, len(parentIndex)+1)
		index = append(index, parentIndex...)
		index = append(index, i)

		// See whether this is an embedded field
		if f.Anonymous {
			jsonTag := f.Tag.Get("json")
			if jsonTag == "-" {
				continue
			}
			if jsonTag == "" {
				fields = appendFields(fields, index, f.Type)
				continue iteration
			}
		}

		// Ignore certain types
		switch f.Type.Kind() {
		case reflect.Func, reflect.Chan:
			continue iteration
		}

		// Is it a private (lowercase) field?
		firstRune, _ := utf8.DecodeRuneInString(f.Name)
		if unicode.IsLower(firstRune) {
			continue iteration
		}

		// Declare a field
		field := theFieldInfo{
			Index:    index,
			Type:     f.Type,
			JSONName: f.Name,
		}

		// Read "json" tag
		jsonTag := f.Tag.Get("json")

		// Handle "-"
		if jsonTag == "-" {
			continue
		}

		// Parse the tag
		if jsonTag != "" {
			field.HasJSONTag = true
			first := true
			for part := range strings.SplitSeq(jsonTag, ",") {
				if first {
					if part != "" {
						field.JSONName = part
					}
				} else {
					switch part {
					case "omitempty":
						field.JSONOmitEmpty = true
					case "string":
						field.JSONString = true
					}
				}
				first = false
			}
		}

		_, field.TypeIsMarshaler = field.Type.MethodByName("MarshalJSON")
		_, field.TypeIsUnmarshaler = field.Type.MethodByName("UnmarshalJSON")

		// Field is done
		fields = append(fields, field)
	}

	return fields
}
