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

import (
	"errors"
	atr "simrs-vx/internal/domain/main-entities/ambulance-transport-req"
	eren "simrs-vx/internal/domain/references/encounter"
	erp "simrs-vx/internal/domain/references/person"
	pl "simrs-vx/pkg/logger"
)

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

	data.Patient_Id = inputSrc.Patient_Id
	data.Diagnoses = inputSrc.Diagnoses
	data.RequestDate = inputSrc.RequestDate
	data.UsageDate = inputSrc.UsageDate

	data.Address = inputSrc.Address
	data.RtRw = inputSrc.RtRw
	data.Province_Code = inputSrc.Province_Code
	data.Regency_Code = inputSrc.Regency_Code
	data.District_Code = inputSrc.District_Code
	data.Village_Code = inputSrc.Village_Code

	data.Facility_Code = inputSrc.Facility_Code
	data.Needs_Code = inputSrc.Needs_Code
	data.Contact_Name = inputSrc.Contact_Name
	data.Contact_Relationship_Code = inputSrc.Contact_Relationship_Code
	data.Contact_PhoneNumber = inputSrc.Contact_PhoneNumber
}

func validateRequestCode(i *atr.CreateDto, event *pl.Event) (err error) {
	event.Status = "failed"
	event.ErrInfo = pl.ErrorInfo{
		Code: "data-validation-fail",
	}

	// validate facility code value
	if i.Facility_Code != nil {
		if *i.Facility_Code != eren.AFCStd && *i.Facility_Code != eren.AFCIcu {
			event.ErrInfo.Detail = "facilty_code value is not recognized"
			event.ErrInfo.Raw = errors.New("facilty_code value is not recognized")
			return pl.SetLogError(event, i)
		}
	}

	// validate needs code value
	if i.Needs_Code != nil {
		if *i.Needs_Code != eren.ANCAssist && *i.Needs_Code != eren.ANCNonassist {
			event.ErrInfo.Detail = "needs_code value is not recognized"
			event.ErrInfo.Raw = errors.New("needs_code value is not recognized")
			return pl.SetLogError(event, i)
		}
	}

	// validate contact relationship code value
	if i.Contact_Relationship_Code != nil {
		switch *i.Contact_Relationship_Code {
		case erp.RCMother, erp.RCFather, erp.RCUncle, erp.RCAunt,
			erp.RCSibling, erp.RCGdMother, erp.RCGdFather, erp.RCChild,
			erp.RCNephew, erp.RCGdChild, erp.RCOther:
			return
		default:
			event.ErrInfo.Detail = "contact_relationship_code value is not recognized"
			event.ErrInfo.Raw = errors.New("contact_relationship_code value is not recognized")
			return pl.SetLogError(event, i)
		}
	}
	return
}
