package medicationrequest

import (
	"encoding/json"
	"fmt"
	"time"
)

// TODO: Sesuaikan dengan payload internal API MedicationRequest
func MapToInternalAPI(dbData *MedicationRequestDB, orgID string) map[string]interface{} {
	waktu := time.Now()
	if dbData.TglResep.Valid {
		waktu = dbData.TglResep.Time
	}
	return map[string]interface{}{
		"medicationrequest_id": fmt.Sprintf("%d", dbData.IdxResep),
		"encounter_id":         fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":           dbData.NoMR.String,
		"medication_code":      dbData.KFA.String,
		"authored_on":          waktu.Format(time.RFC3339),
		"dosage_text":          dbData.Instruksi.String,
	}
}

func MapToSyncLog(idx int64, fhirID string, reqPayload interface{}, respBody []byte, status string, errMsg string) MedicationRequestSyncLog {
	reqBytes, _ := json.MarshalIndent(reqPayload, "", "  ")
	return MedicationRequestSyncLog{
		IdxResep:            idx,
		MedicationRequestID: fhirID,
		RequestPayload:      string(reqBytes),
		ResponsePayload:     string(respBody),
		Status:              status,
		ErrorMessage:        errMsg,
	}
}
