package daemons

import (
	"fmt"
	"oc-schedulerd/conf"
	"os/exec"

	"github.com/rs/zerolog"
)

type LocalMonitor struct {
	LokiURL      string
	KubeURL      string
	WorkflowName string
	Logger       zerolog.Logger
}

func (lm *LocalMonitor) LaunchLocalMonitor() {
	if lm.LokiURL == "" || lm.KubeURL == "" || lm.WorkflowName == "" {
		lm.Logger.Error().Msg("Missing parameter in LocalMonitor")
	}

	// For dev purposes, in prod KubeURL must be a kube API's URL
	if lm.KubeURL != "localhost" {
		lm.execRemoteKube()
	} else {
		lm.execLocalKube()
	}
}

func (lm *LocalMonitor) execLocalKube() {
	cmd := exec.Command(conf.GetConfig().MonitorPath, "-w", lm.WorkflowName, "-u", lm.LokiURL, "-m", conf.GetConfig().MongoUrl, "-d", conf.GetConfig().DBName)
	fmt.Println("CMD", cmd)
	err := cmd.Start()
	if err != nil {
		lm.Logger.Error().Msg("Could not start oc-monitor for " + lm.WorkflowName + " : " + err.Error())
	}
}

// TODO : implement this
func (lm *LocalMonitor) execRemoteKube() {

}
