package api

import (
	"context"
	"encoding/json"
	"fmt"

	"google.golang.org/protobuf/types/known/emptypb"

	"deevirt.fr/compute/pkg/api/proto"
	"deevirt.fr/compute/pkg/api/raft"
	"deevirt.fr/compute/pkg/config"
)

type Node struct {
	Config *config.Config
	Store  *raft.Store
	proto.UnimplementedNodeServer
}

func (d *Node) LibvirtQemu(ctx context.Context, in *proto.NodeLibvirtQemuRequest) (*emptypb.Empty, error) {
	node := []struct {
		Uuid    string `json:"uuid"`
		Config  string `json:"config"`
		State   int    `json:"state"`
		Migrate bool   `json:"migrate"`
	}{}

	err := json.Unmarshal(in.Domains, &node)
	if err != nil {
		fmt.Println("Erreur:", err)
	}

	/*for _, n := range node {
		fmt.Printf("%v", n)
	}*/

	d.Store.Set(fmt.Sprintf("/etc/libvirt/qemu/%s/", d.Config.NodeID), in.Domains)

	t, _ := d.Store.Get(fmt.Sprintf("/etc/libvirt/qemu/%s/", d.Config.NodeID))
	fmt.Printf("%v", t)

	return &emptypb.Empty{}, nil
}
