package medicationdispense

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req MedicationDispenseRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("MedicationDispense").
		Set("status", req.Status).
		Set("category", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://terminology.hl7.org/fhir/CodeSystem/medicationdispense-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("context", map[string]interface{}{
			"reference": "Encounter/" + req.EncounterID,
		}).
		Set("performer", []map[string]interface{}{
			{
				"actor": map[string]interface{}{
					"reference": "Practitioner/" + req.PractitionerID,
					"display":   req.PractitionerName,
				},
			},
		}).
		Set("location", map[string]interface{}{
			"reference": "Location/" + req.LocationID,
			"display":   req.LocationName,
		}).
		Set("authorizingPrescription", []map[string]interface{}{
			{"reference": "MedicationRequest/" + req.PrescriptionID},
		}).
		Set("whenPrepared", req.PreparedDate.Format(time.RFC3339)).
		Set("whenHandedOver", req.HandedOverDate.Format(time.RFC3339))

	return payload
}
