package main

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

	"github.com/spf13/cobra"
)

func init() {
	// rootCmd
	rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.bottin.presences.yaml)")

	if err := BindFlags(rootCmd.PersistentFlags()); err != nil {
		log.Fatal(err)
	}
}

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
	Use: "presences",
	//Args:  cobra.(0),
	Short: "Gestion de présences en Assemblée Générale",
	Run: func(c *cobra.Command, args []string) {
		log.Println("Starting bottin/presences microservice")

		ctx := context.TODO()

		cfg, err := ParseConfig()
		if err != nil {
			log.Fatal(err)
		}

		if len(args) > 0 {
			if args[0] == "debugConfig" {
				log.Println("debug: Printing current config")
				cfgAsBytes, err := json.MarshalIndent(cfg, "", "  ")
				if err != nil {
					log.Fatal("Cannot marshal config for printing:", err)
				}
				fmt.Println(string(cfgAsBytes))
			}
		}

		if err := run(ctx, cfg); err != nil {
			log.Fatal(err)
		}
	},
}