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

import (
	"encoding/json"
	erc "simrs-vx/internal/domain/references/common"

	e "simrs-vx/internal/domain/main-entities/division"
	esimgos "simrs-vx/internal/domain/simgos-entities/m-unit"
	esync "simrs-vx/internal/domain/sync-entities/division"
	esyncLog "simrs-vx/internal/domain/sync-entities/log"
)

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

	data.NamaUnit = inputSrc.Name
	return
}

func setDataSimxLog(input *esyncLog.SimxLogDto) (data esync.DivisionSimxLog) {
	// encode to JSON
	jsonData, _ := json.MarshalIndent(input.Payload, "", "  ")
	jsonString := string(jsonData)

	var status erc.ProcessStatusCode
	if input.IsSuccess {
		status = erc.PSCSuccess
	} else {
		status = erc.PSCFailed
		if input.ErrMessage != nil {
			data.ErrMessage = input.ErrMessage
		}
	}

	data.Value = &jsonString
	data.Date = &now
	data.Status = status

	return
}

func setDataSimxLink(simxId, simgosId uint) (data esync.DivisionLink) {
	data.Simx_Id = simxId
	data.Simgos_Id = simgosId
	return
}
