package servicerequest

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req ServiceRequestRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("ServiceRequest").
		Set("status", req.Status).
		Set("intent", req.Intent).
		Set("code", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://snomed.info/sct",
					"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("authoredOn", req.AuthoredOn.Format(time.RFC3339))

	if req.RequesterID != "" {
		payload.Set("requester", map[string]interface{}{
			"reference": "Practitioner/" + req.RequesterID,
			"display":   req.RequesterName,
		})
	}

	return payload
}
