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

import (
	e "simrs-vx/internal/domain/main-entities/device"
	ei "simrs-vx/internal/domain/main-entities/item"

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

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

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

	"gorm.io/gorm"
)

func setData[T *e.CreateDto | *e.UpdateDto](input T, data *e.Device) {
	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.Uom_Code = inputSrc.Uom_Code
	data.Item_Code = inputSrc.Item_Code
	data.Infra_Code = inputSrc.Infra_Code
}

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

	input.Item_Code = &item.Code
	return nil
}
