/*
DESCRIPTION:
Any functions that are used internally by the use-case
*/
package mcusubsrc

import (
	ei "simrs-vx/internal/domain/main-entities/item"
	e "simrs-vx/internal/domain/main-entities/mcu-sub-src"

	ui "simrs-vx/internal/use-case/main-use-case/item"

	pl "simrs-vx/pkg/logger"
	pu "simrs-vx/pkg/use-case-helper"

	ero "simrs-vx/internal/domain/references/organization"

	"gorm.io/gorm"
)

func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.McuSubSrc) {
	var inputSrc *e.CreateDto
	if inputT, ok := any(input).(*e.CreateDto); ok {
		inputSrc = inputT
	} else {
		inputTemp := any(input).(*e.UpdateDto)
		inputSrc = &inputTemp.CreateDto
	}

	data.Code = *inputSrc.Code
	data.Name = inputSrc.Name
	data.McuSrc_Code = inputSrc.McuSrc_Code
	data.Item_Code = inputSrc.Item_Code
}

func createItem(input *e.CreateDto, event *pl.Event, tx *gorm.DB) error {
	itemCreate := ei.CreateDto{
		Code:           pu.AddPrefix("mcuSub-", *input.Code),
		Name:           input.Name,
		ItemGroup_Code: ero.ITGCMCUSub,
		// Uom_Code:       &input.Uom_Code,
		// Infra_Id:       input.Infra_Id,
	}
	item, err := ui.CreateData(itemCreate, event, tx)
	if err != nil {
		return err
	}

	input.Item_Code = &item.Code
	return nil
}
