package attributerepo

import (
	"git.ma-al.com/goc_daniel/b2b/app/db"
	"git.ma-al.com/goc_daniel/b2b/app/model/dbmodel"
)

type AttributeWithColor struct {
	ID    uint
	Name  string
	Color string
}

type AttributeGroupWithAttrs struct {
	GroupName string
	Attrs     map[uint]AttributeWithColor
}

type UIAttributeRepo interface {
	GetAttributeGroupsWithAttributes(groupIDs []uint, attrIDs []uint, idLang uint) (map[uint]AttributeGroupWithAttrs, error)
}

type AttributeRepo struct{}

func New() UIAttributeRepo {
	return &AttributeRepo{}
}

func (r *AttributeRepo) GetAttributeGroupsWithAttributes(groupIDs []uint, attrIDs []uint, idLang uint) (map[uint]AttributeGroupWithAttrs, error) {
	result := make(map[uint]AttributeGroupWithAttrs)

	if len(groupIDs) == 0 && len(attrIDs) == 0 {
		return result, nil
	}

	if len(attrIDs) > 0 {
		type attrResult struct {
			IDAttribute      int32
			IDAttributeGroup int32
			GroupName        string
			AttrName         string
			Color            string
		}

		var attrs []attrResult
		if err := db.Get().
			Model(dbmodel.PsAttribute{}).
			Select(`
				ps_attribute.id_attribute,
				ps_attribute.id_attribute_group,
				COALESCE(ps_attribute_group_lang.name, '') as group_name,
				COALESCE(ps_attribute_lang.name, '') as attr_name,
				ps_attribute.color
			`).
			Joins("LEFT JOIN ps_attribute_lang ON ps_attribute_lang.id_attribute = ps_attribute.id_attribute AND ps_attribute_lang.id_lang = ?", idLang).
			Joins("LEFT JOIN ps_attribute_group_lang ON ps_attribute_group_lang.id_attribute_group = ps_attribute.id_attribute_group AND ps_attribute_group_lang.id_lang = ?", idLang).
			Where("ps_attribute.id_attribute IN ?", attrIDs).
			Scan(&attrs).Error; err != nil {
			return nil, err
		}

		for _, a := range attrs {
			if _, exists := result[uint(a.IDAttributeGroup)]; !exists {
				result[uint(a.IDAttributeGroup)] = AttributeGroupWithAttrs{
					GroupName: a.GroupName,
					Attrs:     make(map[uint]AttributeWithColor),
				}
			}
			result[uint(a.IDAttributeGroup)].Attrs[uint(a.IDAttribute)] = AttributeWithColor{
				ID:    uint(a.IDAttribute),
				Name:  a.AttrName,
				Color: a.Color,
			}
		}
	}

	if len(groupIDs) > 0 {
		type groupResult struct {
			IDAttributeGroup int32
			GroupName        string
		}

		var groups []groupResult
		if err := db.Get().
			Model(dbmodel.PsAttributeGroupLang{}).
			Select("ps_attribute_group_lang.id_attribute_group, COALESCE(ps_attribute_group_lang.name, '') as group_name").
			Where("ps_attribute_group_lang.id_attribute_group IN ?", groupIDs).
			Where("ps_attribute_group_lang.id_lang = ?", idLang).
			Scan(&groups).Error; err != nil {
			return nil, err
		}

		for _, g := range groups {
			if existing, ok := result[uint(g.IDAttributeGroup)]; ok {
				if g.GroupName != "" {
					existing.GroupName = g.GroupName
					result[uint(g.IDAttributeGroup)] = existing
				}
			} else {
				result[uint(g.IDAttributeGroup)] = AttributeGroupWithAttrs{
					GroupName: g.GroupName,
					Attrs:     make(map[uint]AttributeWithColor),
				}
			}
		}
	}

	return result, nil
}
