package servicerequest

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

// TODO: Sesuaikan dengan payload internal API ServiceRequest
func MapToInternalAPI(dbData *ServiceRequestDB, orgID string) map[string]interface{} {
	waktu := time.Now()
	if dbData.TglOrder.Valid {
		waktu = dbData.TglOrder.Time
	}
	payload := map[string]interface{}{
		"servicerequest_id": fmt.Sprintf("%d", dbData.IdxOrder),
		"encounter_id":      fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":        dbData.NoMR.String,
		"code":              dbData.KdOrder.String,
		"authored_on":       waktu.Format(time.RFC3339),
	}
	return payload
}

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