package condition

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

// MapToInternalAPI memetakan data database menjadi payload untuk Internal API
// TODO: Sesuaikan dengan JSON Payload endpoint Condition Anda
func MapToInternalAPI(dbData *ConditionDB, orgID string) map[string]interface{} {
	waktuDiagnosa := time.Now()
	if dbData.TglDiagnosa.Valid {
		waktuDiagnosa = dbData.TglDiagnosa.Time
	}

	payload := map[string]interface{}{
		"condition_id":  fmt.Sprintf("%d", dbData.IdxDiagnosa),
		"encounter_id":  fmt.Sprintf("%d", dbData.IdxDaftar),
		"patient_id":    dbData.NoMR.String, // Perlu diganti IHS Number di service
		"code":          dbData.KdICD10.String,
		"display":       dbData.NamaPenyakit.String,
		"recorded_date": waktuDiagnosa.Format(time.RFC3339),
	}
	return payload
}

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