package server

import "odoo-go/pkg/orm"

// fieldsGetForModel returns field metadata for a model.
// Mirrors: odoo/orm/models.py BaseModel.fields_get()
func fieldsGetForModel(modelName string) map[string]interface{} {
	m := orm.Registry.Get(modelName)
	if m == nil {
		return map[string]interface{}{}
	}

	result := make(map[string]interface{})
	for name, f := range m.Fields() {
		// Never expose password fields in metadata
		if name == "password" || name == "password_crypt" {
			continue
		}
		fType := f.Type.String()

		fieldInfo := map[string]interface{}{
			"name":           name,
			"type":           fType,
			"string":         f.String,
			"help":           f.Help,
			"readonly":       f.Readonly,
			"required":       f.Required,
			"searchable":     f.IsStored(),
			"sortable":       f.IsStored(),
			"store":          f.IsStored(),
			"manual":         false,
			"depends":        f.Depends,
			"groupable":      f.IsStored() && f.Type != orm.TypeText && f.Type != orm.TypeHTML,
			"exportable":     true,
			"change_default": false,
			"company_dependent": false,
		}

		// Relational fields
		if f.Comodel != "" {
			fieldInfo["relation"] = f.Comodel
		}
		if f.InverseField != "" {
			fieldInfo["relation_field"] = f.InverseField
		}

		// Selection
		if f.Type == orm.TypeSelection && len(f.Selection) > 0 {
			sel := make([][]string, len(f.Selection))
			for i, item := range f.Selection {
				sel[i] = []string{item.Value, item.Label}
			}
			fieldInfo["selection"] = sel
		}

		// Monetary fields need currency_field
		if f.Type == orm.TypeMonetary {
			cf := f.CurrencyField
			if cf == "" {
				cf = "currency_id"
			}
			fieldInfo["currency_field"] = cf
		}

		// Computed fields
		if f.Compute != "" {
			fieldInfo["compute"] = f.Compute
		}
		if f.Related != "" {
			fieldInfo["related"] = f.Related
		}

		// Aggregator hint for read_group
		// Mirrors: odoo/orm/fields.py Field.group_operator
		switch f.Type {
		case orm.TypeInteger, orm.TypeFloat, orm.TypeMonetary:
			fieldInfo["aggregator"] = "sum"
			fieldInfo["group_operator"] = "sum"
		case orm.TypeBoolean:
			fieldInfo["aggregator"] = "bool_or"
			fieldInfo["group_operator"] = "bool_or"
		default:
			fieldInfo["aggregator"] = nil
			fieldInfo["group_operator"] = nil
		}

		// Default domain & context
		fieldInfo["domain"] = []interface{}{}
		fieldInfo["context"] = map[string]interface{}{}

		result[name] = fieldInfo
	}

	return result
}
