package medicationrequest

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req MedicationRequestRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("MedicationRequest").
		Set("status", req.Status).
		Set("intent", req.Intent).
		Set("category", []map[string]interface{}{
			{
				"coding": []map[string]interface{}{
					{
						"system":  "http://terminology.hl7.org/CodeSystem/medicationrequest-category",
						"code":    "outpatient",
						"display": "Outpatient",
					},
				},
			},
		}).
		Set("medicationReference", map[string]interface{}{
			"reference": "Medication/" + req.MedicationID,
			"display":   req.MedicationDisplay,
		}).
		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)).
		Set("requester", map[string]interface{}{
			"reference": "Practitioner/" + req.PractitionerID,
			"display":   req.PractitionerName,
		}).
		Set("dosageInstruction", []map[string]interface{}{
			{
				"sequence":           1,
				"text":               req.DosageText,
				"patientInstruction": req.PatientInstr,
			},
		}).
		Set("dispenseRequest", map[string]interface{}{
			"quantity": map[string]interface{}{
				"value": req.DispenseValue,
				"code":  req.DispenseUnit,
			},
			"expectedSupplyDuration": map[string]interface{}{
				"value":  req.SupplyDuration,
				"unit":   "days",
				"system": "http://unitsofmeasure.org",
				"code":   "d",
			},
		})
	return payload
}
