package medication

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req MedicationRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("Medication").
		Set("status", req.StatusCode).
		Set("code", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://sys-ids.kemkes.go.id/kfa",
					"code":    req.KfaCode,
					"display": req.KfaDisplay,
				},
			},
		})

	if req.FormCode != "" {
		payload.Set("form", map[string]interface{}{
			"coding": []map[string]interface{}{
				{
					"system":  "http://terminology.kemkes.go.id/CodeSystem/medication-form",
					"code":    req.FormCode,
					"display": req.FormDisplay,
				},
			},
		})
	}

	if req.ManufacturerID != "" {
		payload.Set("manufacturer", map[string]interface{}{
			"reference": "Organization/" + req.ManufacturerID,
		})
	}

	if req.BatchNumber != "" && req.ExpirationDate != nil {
		payload.Set("batch", map[string]interface{}{
			"lotNumber":      req.BatchNumber,
			"expirationDate": req.ExpirationDate.Format(time.RFC3339),
		})
	}

	return payload
}
