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

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

	e "simrs-vx/internal/domain/main-entities/unit"

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

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

	data.Nama = inputSrc.Name
	data.Jenispoly = 0

	kodePoly, _ := strconv.Atoi(inputSrc.Code)
	data.Kode = uint(kodePoly)
	return
}

func setDataSimxLog(input *esyncLog.SimxLogDto) (data esync.UnitSimxLog) {
	// 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.UnitLink) {
	data.Simx_Id = simxId
	data.Simgos_Id = simgosId
	return
}
