package imagingstudy

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req ImagingStudyRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("ImagingStudy").
		Set("status", req.Status).
		Set("subject", map[string]interface{}{
			"reference": "Patient/" + req.PatientID,
			"display":   req.PatientName,
		}).
		Set("encounter", map[string]interface{}{
			"reference": "Encounter/" + req.EncounterID,
		}).
		Set("started", req.Started.Format(time.RFC3339)).
		Set("numberOfSeries", req.NumberOfSeries).
		Set("numberOfInstances", req.NumberOfInstances).
		Set("description", req.Description)

	if req.ProcedureCode != "" {
		payload.Set("procedureCode", []map[string]interface{}{
			{
				"coding": []map[string]interface{}{
					{
						"system":  "http://snomed.info/sct",
						"code":    req.ProcedureCode,
						"display": req.ProcedureDisplay,
					},
				},
			},
		})
	}

	return payload
}
