package scheduler

import (
	"context"
	"fmt"
	"log"
	"regexp"
	"strings"
	"time"

	v1 "github.com/prometheus/client_golang/api/prometheus/v1"
	"github.com/prometheus/common/model"
	clientv3 "go.etcd.io/etcd/client/v3"
)

type nodeDown struct {
	node_id string
	domains []string
}

func (w *Scheduler) checkHA() []nodeDown {
	s := []nodeDown{}

	etcd, err := clientv3.New(clientv3.Config{
		Endpoints:   strings.Split(w.config.EtcdURI, ","),
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		log.Fatalf("Error connexion to etcd: %v", err)
	}
	defer etcd.Close()

	r := v1.Range{
		Start: time.Now().Add(-time.Minute),
		End:   time.Now(),
		Step:  2 * time.Minute,
	}

	api, err := w.api()
	if err != nil {
		return s
	}

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()

	println("up{cluster_id=" + w.config.ClusterID + "}")

	result, warnings, err := api.QueryRange(ctx, "up{cluster_id='"+w.config.ClusterID+"'}", r, v1.WithTimeout(5*time.Second))
	if err != nil {
		fmt.Printf("Error querying Prometheus: %v\n", err)
	}
	if len(warnings) > 0 {
		fmt.Printf("Warnings: %v\n", warnings)
	}

	matrix, _ := result.(model.Matrix)

	for _, stream := range matrix {
		node_id := ""
		domains := []string{}

		for key, value := range stream.Metric {
			if key == "node_id" {
				//test.instance = string(value)
				node_id = string(value)
			}

		}

		state := int(stream.Values[0].Value)

		if state == 1 {
			re := regexp.MustCompile(`qemu/(?P<domainID>[a-zA-Z0-9-]+)`)

			resp, _ := etcd.Get(ctx, "/cluster/"+w.config.ClusterID+"/host/"+node_id+"/qemu/", clientv3.WithPrefix(), clientv3.WithKeysOnly())
			for _, kv := range resp.Kvs {
				matches := re.FindStringSubmatch(string(kv.Key))
				if matches != nil {
					index := re.SubexpIndex("domainID")
					domains = append(domains, matches[index])
				}
			}

			s = append(s, nodeDown{
				node_id: node_id,
				domains: domains,
			})
		}

		/*for _, pair := range stream.Values {
			println(pair.Value.String())
		}*/
	}

	return s
}
