package logger

import (
	"bufio"
	"encoding/json"
	"fmt"
	"io"
	"oc-monitord/conf"

	"strings"
	"sync"

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

var logger zerolog.Logger
var wfLogger zerolog.Logger


// Take the slice of string that make up one round of stderr outputs from the --watch option in argo submit
func NewLocalArgoWatch(inputs []string) *ArgoWatch {
	var workflow ArgoWatch

	for _, input := range inputs {
		line := strings.TrimSpace(input)
		if line == "" {
			continue
		}
		switch {
		case strings.HasPrefix(line, "Name:"):
			workflow.Name = parseValue(line)
		case strings.HasPrefix(line, "Namespace:"):
			workflow.Namespace = parseValue(line)
		case strings.HasPrefix(line, "Status:"):
			workflow.Status = parseValue(line)
		case strings.HasPrefix(line, "PodRunning"):
			workflow.PodRunning = parseBoolValue(line)
		case strings.HasPrefix(line, "Completed"):
			workflow.Completed = parseBoolValue(line)
		case strings.HasPrefix(line, "Created:"):
			workflow.Created = parseValue(line)
		case strings.HasPrefix(line, "Started:"):
			workflow.Started = parseValue(line)
		case strings.HasPrefix(line, "Duration:"):
			workflow.Duration = parseValue(line)
		case strings.HasPrefix(line, "Progress:"):
			workflow.Progress = parseValue(line)
		}
	}

	return &workflow
}



func parseValue(line string) string {
	parts := strings.SplitN(line, ":", 2)
	if len(parts) < 2 {
		return ""
	}
	return strings.TrimSpace(parts[1])
}

func parseBoolValue(line string) bool {
	value := parseValue(line)
	return value == "True"
}

func LogLocalWorkflow(wfName string, pipe io.ReadCloser, wg *sync.WaitGroup) {
	logger = logs.GetLogger()

	logger.Debug().Msg("created wf_logger")
	fmt.Println("created wf_logger")
	wfLogger = logger.With().Str("argo_name", wfName).Str("workflow_id", conf.GetConfig().WorkflowID).Str("workflow_execution_id", conf.GetConfig().ExecutionID).Logger()

	var current_watch, previous_watch ArgoWatch

	watch_output := make([]string, 0)
	scanner := bufio.NewScanner(pipe)
	for scanner.Scan() {
		log := scanner.Text()
		watch_output = append(watch_output, log)

		// Log the progress of the WF
		if strings.HasPrefix(log, "Progress:") {

			current_watch = *NewLocalArgoWatch(watch_output)
			workflowName := current_watch.Name
			if !current_watch.Equals(&previous_watch) {
				wg.Add(1)
				// checkStatus(current_watch.Status, previous_watch.Status)
				jsonified, err := json.Marshal(current_watch)
				if err != nil {
					logger.Error().Msg("Could not create watch log for " + workflowName)
				}
				wfLogger.Info().Msg(string(jsonified))
				previous_watch = current_watch
				current_watch = ArgoWatch{}
				wg.Done()
			}
		}



	}
}

// Debug, no logs sent
func LogLocalPod(wfName string, pipe io.ReadCloser, steps []string, wg *sync.WaitGroup) {
	scanner := bufio.NewScanner(pipe)
	for scanner.Scan() {
		var podLogger zerolog.Logger
		fmt.Println("new line")
		wg.Add(1)
		
		line := scanner.Text()
		podName := strings.Split(line, ":")[0]
		podLogger = wfLogger.With().Str("step_name", getStepName(podName, steps)).Logger()
		log := strings.Split(line,podName+":")[1]
		podLog := NewArgoPodLog(wfName,podName,log)
		
		jsonifiedLog, err := json.Marshal(podLog)
		if err != nil {
			podLogger.Fatal().Msg(err.Error())
		}

		podLogger.Info().Msg(string(jsonifiedLog))
		wg.Done()
	}

}

func getStepName(podName string, steps []string) string {

	for _, step := range(steps) {
		if strings.Contains(podName,step){
			return step
		}
	}

	return "error"
}

