package observation

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req ObservationRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("Observation").
		Set("status", req.Status).
		Set("category", []map[string]interface{}{
			{
				"coding": []map[string]interface{}{
					{
						"system":  "http://terminology.hl7.org/CodeSystem/observation-category",
						"code":    req.CategoryCode,
						"display": req.CategoryDisplay,
					},
				},
			},
		}).
		Set("code", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://loinc.org",
					"code":    req.Code,
					"display": req.Display,
				},
			},
		}).
		Set("subject", map[string]interface{}{
			"reference": "Patient/" + req.PatientID,
			"display":   req.PatientName,
		}).
		Set("encounter", map[string]interface{}{
			"reference": "Encounter/" + req.EncounterID,
		}).
		Set("effectiveDateTime", req.EffectiveDateTime.Format(time.RFC3339)).
		Set("valueQuantity", map[string]interface{}{
			"value":  req.Value,
			"unit":   req.Unit,
			"system": "http://unitsofmeasure.org",
			"code":   req.UnitCode,
		})

	return payload
}
