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

import (
	ei "simrs-vx/internal/domain/main-entities/item"
	e "simrs-vx/internal/domain/main-entities/medical-action-src"

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

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

	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.MedicalActionSrc) {
	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.Type_Code = inputSrc.Type_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("medAct-", *input.Code),
		Name:           input.Name,
		ItemGroup_Code: ero.ITGCMedAct,
	}
	item, err := ui.CreateData(itemCreate, event, tx)
	if err != nil {
		return err
	}

	input.Item_Code = &item.Code
	return nil
}
