package specimen

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

// TODO: Sesuaikan dengan payload internal API Specimen
func MapToInternalAPI(dbData *SpecimenDB, orgID string) map[string]interface{} {
	waktu := time.Now()
	if dbData.TglAmbil.Valid {
		waktu = dbData.TglAmbil.Time
	}
	payload := map[string]interface{}{
		"specimen_id":    fmt.Sprintf("%d", dbData.IdxSpesimen),
		"encounter_id":   fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":     dbData.NoMR.String,
		"specimen_type":  dbData.JenisSpesimen.String,
		"collected_date": waktu.Format(time.RFC3339),
	}
	return payload
}

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