package episodeofcare

import (
	"time"

	"service/internal/interfaces/satusehat"
)

func MapRequestToFHIR(req EpisodeOfCareRequest) satusehat.FHIRPayload {
	payload := satusehat.NewFHIRPayload("EpisodeOfCare").
		Set("status", req.Status).
		Set("patient", map[string]interface{}{
			"reference": "Patient/" + req.PatientID,
			"display":   req.PatientName,
		})

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

	period := map[string]interface{}{"start": req.PeriodStart.Format(time.RFC3339)}
	if req.PeriodEnd != nil {
		period["end"] = req.PeriodEnd.Format(time.RFC3339)
	}
	payload.Set("period", period)

	return payload
}
