package medicationdispense

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

// TODO: Sesuaikan dengan payload internal API MedicationDispense
func MapToInternalAPI(dbData *MedicationDispenseDB, orgID string) map[string]interface{} {
	waktu := time.Now()
	if dbData.TglDiberikan.Valid {
		waktu = dbData.TglDiberikan.Time
	}
	return map[string]interface{}{
		"medicationdispense_id": fmt.Sprintf("%d", dbData.IdxPemberian),
		"encounter_id":          fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":            dbData.NoMR.String,
		"medication_code":       dbData.KFA.String,
		"when_handed_over":      waktu.Format(time.RFC3339),
		"quantity":              dbData.Qty.Float64,
	}
}

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