package medication

import (
	"encoding/json"
	"strconv"
)

// MapToInternalAPI memetakan data DB ke payload API Internal Medication
func MapToInternalAPI(dbData *MedicationDB, orgID string, kfaDisplay, formCode, formDisplay string) map[string]interface{} {
	payload := make(map[string]interface{})

	payload["status_code"] = "active"

	if dbData.IdxPesanObat != 0 {
		payload["medication_id"] = strconv.FormatInt(dbData.IdxPesanObat, 10)
	}
	if dbData.KfaCode.Valid && dbData.KfaCode.String != "" {
		payload["kfa_code"] = dbData.KfaCode.String
	}
	if orgID != "" {
		payload["manufacturer_id"] = orgID
	}

	if kfaDisplay != "" {
		payload["kfa_display"] = kfaDisplay
	}
	if formCode != "" {
		payload["form_code"] = formCode
	}
	if formDisplay != "" {
		payload["form_display"] = formDisplay
	}
	return payload
}

func MapToSyncLog(idx int64, fhirID string, reqPayload interface{}, respBody []byte, status string, errMsg string) MedicationSyncLog {
	reqBytes, _ := json.Marshal(reqPayload)
	return MedicationSyncLog{
		IdxPesanObat:    idx,
		SatuSehatID:     fhirID,
		RequestPayload:  string(reqBytes),
		ResponsePayload: string(respBody),
		Status:          status,
		ErrorMessage:    errMsg,
	}
}
