package medicationstatement

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req MedicationStatementRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("MedicationStatement").
		Set("status", req.Status).
		Set("category", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://terminology.hl7.org/CodeSystem/medication-statement-category",
					"code":    req.CategoryCode,
					"display": req.CategoryCode,
				},
			},
		}).
		Set("medicationCodeableConcept", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://sys-ids.kemkes.go.id/kfa",
					"code":    req.MedicationCode,
					"display": req.MedicationDisplay,
				},
			},
		}).
		Set("subject", map[string]interface{}{
			"reference": "Patient/" + req.PatientID,
			"display":   req.PatientName,
		}).
		Set("context", map[string]interface{}{
			"reference": "Encounter/" + req.EncounterID,
		}).
		Set("informationSource", map[string]interface{}{
			"reference": "Patient/" + req.PatientID,
			"display":   req.PatientName,
		}).
		Set("effectiveDateTime", req.EffectiveDateTime.Format(time.RFC3339)).
		Set("dateAsserted", req.DateAsserted.Format(time.RFC3339)).
		Set("dosage", []map[string]interface{}{
			{
				"text": req.DosageText,
			},
		})

	return payload
}
