package dto

import (
	entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
	uomDTO "gitlab.com/mbugroup/lti-api.git/internal/modules/master/uoms/dto"
)

// === DTO Structs ===

type SupplierProductDTO struct {
	Id            uint                   `json:"id"`
	Name          string                 `json:"name"`
	ProductPrice  float64                `gorm:"type:numeric(15,3);not null"`
	SellingPrice  *float64               `gorm:"type:numeric(15,3)"`
	SupplierPrice float64                `json:"supplier_price"`
	Uom           *uomDTO.UomRelationDTO `json:"uom,omitempty"`
	Flags         []string               `json:"flags"`
}

// === Mapper Functions ===

func toSupplierProductDTOs(relations []entity.ProductSupplier) []SupplierProductDTO {
	if len(relations) == 0 {
		return nil
	}

	result := make([]SupplierProductDTO, 0, len(relations))
	for _, relation := range relations {
		product := relation.Product
		if product.Id == 0 {
			continue
		}
		if len(product.Flags) == 0 {
			continue
		}

		flags := make([]string, len(product.Flags))
		for i, f := range product.Flags {
			flags[i] = f.Name
		}

		var uomRef *uomDTO.UomRelationDTO
		if product.Uom.Id != 0 {
			mapped := uomDTO.ToUomRelationDTO(product.Uom)
			uomRef = &mapped
		}

		result = append(result, SupplierProductDTO{
			Id:            product.Id,
			Name:          product.Name,
			ProductPrice:  product.ProductPrice,
			SellingPrice:  product.SellingPrice,
			SupplierPrice: relation.Price,
			Uom:           uomRef,
			Flags:         flags,
		})
	}
	return result
}
