package procedure

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req ProcedureRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("Procedure").
		Set("status", req.Status).
		Set("category", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://snomed.info/sct",
					"code":    req.CategoryCode,
					"display": req.CategoryDisplay,
				},
			},
		}).
		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("performedDateTime", req.PerformedDateTime.Format(time.RFC3339))

	if req.PractitionerID != "" {
		payload.Append("performer", map[string]interface{}{
			"actor": map[string]interface{}{
				"reference": "Practitioner/" + req.PractitionerID,
				"display":   req.PractitionerName,
			},
		})
	}

	return payload
}
