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

import (
	pl "simrs-vx/pkg/logger"
	pmh "simrs-vx/pkg/minio-helper"

	e "simrs-vx/internal/domain/main-entities/encounter-document"
)

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

	data.Encounter_Id = inputSrc.Encounter_Id
	data.Type_Code = inputSrc.Type_Code
	data.Name = inputSrc.Name
	data.FilePath = &inputSrc.FilePath
	data.FileName = &inputSrc.Filename
	data.Upload_Employee_Id = inputSrc.Upload_Employee_Id
}

func removeUploadedFile(bucket, fileUrl string, event *pl.Event) error {
	pl.SetLogInfo(event, nil, "started", "removeUploadedFile")
	filename, err := pmh.GetFilename(fileUrl)
	if err != nil {
		return err
	}

	err = pmh.I.RemoveObject(bucket, filename)
	if err != nil {
		return err
	}
	pl.SetLogInfo(event, nil, "complete")
	return nil
}
