package questionnaireresponse

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

// TODO: Sesuaikan dengan payload internal API QuestionnaireResponse
func MapToInternalAPI(dbData *QuestionnaireResponseDB, orgID string) map[string]interface{} {
	waktu := time.Now()
	if dbData.TglIsi.Valid {
		waktu = dbData.TglIsi.Time
	}
	return map[string]interface{}{
		"questionnaireresponse_id": fmt.Sprintf("%d", dbData.IdxKuesioner),
		"encounter_id":             fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":               dbData.NoMR.String,
		"authored_on":              waktu.Format(time.RFC3339),
	}
}

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