package main

import (
	"os"
	"xyj-figures/pkg/batch"

	log "github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
)

var (
	rootCmd = &cobra.Command{
		Use:   "xyj-figures",
		Short: "A binary to generate map chart for figures in 西游记",
		Run: func(cmd *cobra.Command, args []string) {
			es, rs, errs := batch.LoadFolder("data")
			if errs != nil {
				log.Errorf("unable to load data on folder %s: %v", "data", errs)
			}
			log.Infof("loaded %d entities and %d relationships", len(es), len(rs))

			ns, rs, errs := batch.DistillData(es, rs)
			if errs != nil {
				log.Errorf("unable to distill data: %v", errs)
			}

			errs = batch.Save("output/output.dot", ns, rs)
			if errs != nil {
				log.Errorf("unable to save dot: %v", errs)
			}
		},
	}
)

// Execute executes the root command.
func Execute() error {
	return rootCmd.Execute()
}

func main() {
	log.SetFormatter(&log.JSONFormatter{})

	// Output to stdout instead of the default stderr
	// Can be any io.Writer, see below for File example
	log.SetOutput(os.Stdout)

	// Only log the warning severity or above.
	log.SetLevel(log.InfoLevel)

	if err := rootCmd.Execute(); err != nil {
		log.Fatalf("failed to exec cmd: %v", err)
		os.Exit(1)
	}
}
