package utils

import (
	"oc-monitord/conf"
	"sync"

	oclib "cloud.o-forge.io/core/oc-lib"
	"cloud.o-forge.io/core/oc-lib/logs"
	"cloud.o-forge.io/core/oc-lib/models/workflow_execution"
	"github.com/rs/zerolog"
)

var (
	logger 		zerolog.Logger
	wf_logger 	zerolog.Logger
 	pods_logger zerolog.Logger
	onceLogger 	sync.Once
	onceWF 		sync.Once
)
func GetExecution(exec_id string) *workflow_execution.WorkflowExecution {
	res := oclib.NewRequest(oclib.LibDataEnum(oclib.WORKFLOW_EXECUTION), "", conf.GetConfig().PeerID, []string{}, nil).LoadOne(exec_id)
	if res.Code != 200 {
		logger := oclib.GetLogger()
		logger.Error().Msg("Could not retrieve workflow ID from execution ID " + exec_id)
		return nil
	}
	return res.ToWorkflowExecution()
}

func GetLogger() zerolog.Logger {
	onceLogger.Do(func(){
		logger = logs.CreateLogger("oc-monitord")
	})
	return logger
}

func GetWFLogger(workflowName string) zerolog.Logger {
	onceWF.Do(func(){
		wf_logger = logger.With().
							Str("argo_name", workflowName).
							Str("workflow_id", conf.GetConfig().
							WorkflowID).Str("workflow_execution_id", conf.GetConfig().ExecutionID).Logger()
	})
	return wf_logger
}