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

import (
	ei "simrs-vx/internal/domain/main-entities/item"
	e "simrs-vx/internal/domain/main-entities/mcu-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.McuSrc) {
	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.McuSrcCategory_Code = inputSrc.McuSrcCategory_Code
}

func createItem(input *e.CreateDto, event *pl.Event, tx *gorm.DB) error {
	itemCreate := ei.CreateDto{
		Code:           pu.AddPrefix("mcu-", *input.Code),
		Name:           input.Name,
		ItemGroup_Code: ero.ITGCMCU,
		// 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
}
