package medicationrequest

import (
	"encoding/json"
	"time"

	"service/pkg/logger"
)

func MapToInternalAPI(dbData *MedicationRequestDB, orgID string) map[string]interface{} {
	waktuStr := ""
	if !dbData.CreatedAt.IsZero() {
		waktuStr = dbData.CreatedAt.Format(time.RFC3339)
	} else {
		waktuStr = logger.LocalNow().Format(time.RFC3339)
	}

	return map[string]interface{}{
		"medicationrequest_id": dbData.No,
		"patient_id":           dbData.PatientID,
		"encounter_id":         dbData.EncounterID,
		"status":               "active",
		"intent":               "order",
		"medication_id":        dbData.MedicationID,
		"practitioner_id":      dbData.PractitionerID,
		"patient_instruction":  dbData.AturanPakai,
		"authored_on":          waktuStr,
		"dispense_value":       dbData.JmlhKeluar,
		"dispense_unit":        dbData.Sediaan,
	}
}

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