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

import (
	e "simrs-vx/internal/domain/main-entities/infra"
	ei "simrs-vx/internal/domain/main-entities/item"
	er "simrs-vx/internal/domain/main-entities/procedure-room"
	ero "simrs-vx/internal/domain/references/organization"
	ui "simrs-vx/internal/use-case/main-use-case/item"
	ur "simrs-vx/internal/use-case/main-use-case/procedure-room"
	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.Infra) {
	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.InfraGroup_Code = inputSrc.InfraGroup_Code
	data.Parent_Code = inputSrc.Parent_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("inf-", input.Code),
		Name:           input.Name,
		ItemGroup_Code: ero.ITGCInfra,
		Uom_Code: func() *string {
			tmp := "unit"
			return &tmp
		}(),
		Infra_Code: input.Parent_Code,
	}
	item, err := ui.CreateData(itemCreate, event, tx)
	if err != nil {
		return err
	}

	input.Item_Code = &item.Code
	return nil
}

func createProcedureRoom(input *e.CreateDto, event *pl.Event, tx *gorm.DB) error {
	roomCreate := er.CreateDto{
		Code:               input.Infra_Code,
		Infra_Code:         input.Infra_Code,
		Unit_Code:          input.Unit_Code,
		Specialist_Code:    input.Specialist_Code,
		Subspecialist_Code: input.Subspecialist_Code,
	}
	_, err := ur.CreateData(roomCreate, event, tx)
	if err != nil {
		return err
	}
	return nil
}
