package procedure

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

// TODO: Sesuaikan dengan payload internal API Procedure
func MapToInternalAPI(dbData *ProcedureDB, orgID string) map[string]interface{} {
	waktu := time.Now()
	if dbData.TglTindakan.Valid {
		waktu = dbData.TglTindakan.Time
	}
	payload := map[string]interface{}{
		"procedure_id":   fmt.Sprintf("%d", dbData.IdxTindakan),
		"encounter_id":   fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":     dbData.NoMR.String,
		"code":           dbData.KdTindakan.String,
		"performed_date": waktu.Format(time.RFC3339),
	}
	return payload
}

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