package main

import (
	"fmt"
	"github.com/goccy/go-yaml"
	"github.com/spf13/cobra"
	"log"
	"os"
)

type Options struct {
	cni        string
	policyType string
}

func execute(files []string, options *Options) error {
	if len(files) == 0 {
		return fmt.Errorf("File expected")
	}
	config := &Config{}
	for _, file := range files {
		log.Printf("LOADING %s\n", file)
		configNew, err := LoadConfig(file)
		if err != nil {
			return fmt.Errorf("%s: %w", file, err)
		}
		if err = configNew.ValidateSchema(); err != nil {
			return fmt.Errorf("%s: %w", file, err)
		}
		config.Update(configNew)
	}
	err := config.Validate()
	if err != nil {
		return fmt.Errorf("Error loading configuration: %w", err)
	}

	policyTemplates, err := NewPolicyTemplates()
	if err != nil {
		return err
	}
	var generator Generator
	generator = NetworkPolicyGenerrator{
		config:          config,
		policyTemplates: policyTemplates,
	}
	err = Generate(os.Stdout, generator, config)
	if err != nil {
		return err
	}

	return nil
}

func main() {

	val := map[string]string{
		"abc": "1",
	}
	data, err := yaml.Marshal(val)
	if err != nil {
		panic(err)
	}
	log.Printf("val %s", string(data))
	//os.Exit(1)

	options := Options{
		cni:        "cilium",
		policyType: "netpol",
	}
	cmd := &cobra.Command{
		Use:   "policygen",
		Short: "Generate network policies",
		Long:  "Generated policies based on a more compact representation of topology",
		RunE: func(cmd *cobra.Command, args []string) error {
			return execute(args, &options)
		},
	}

	cmd.Execute()
}