package main

import (
	"flag"
	"queencount/api"
	"queencount/bus"
	"queencount/conf"
	"queencount/db"
	"queencount/log"

	"go.uber.org/dig"
	"infish.cn/comm"
)

var confFile = flag.String("conf", "./app.yaml", "conf file")

func BuildApp() *dig.Container {

	app := dig.New()
	_ = app.Provide(func() (*conf.AppConf, error) {
		return conf.NewAppConf(*confFile)
	})

	_ = app.Provide(db.NewMongoDB)
	_ = app.Provide(db.NewRedisClient)
	_ = app.Provide(bus.NewNatsBus)

	// _ = app.Provide(func(app *conf.AppConf) (*etcd.Cli, error) {
	// 	return etcd.NewEtcdCli(&etcd.CliConfig{
	// 		Endpoints:   app.Etcd.Endpoints,
	// 		UserName:    app.Etcd.UserName,
	// 		Password:    app.Etcd.Password,
	// 		DialTimeout: time.Second * time.Duration(app.Etcd.DialTimeout),
	// 	})
	// })

	// repository
	//_ = app.Provide(repository.NewExecuteSnapshotRepository)

	// _ = app.Provide(bus.NewEventBus)
	// _ = app.Provide(collector.NewPinkCollector)
	// _ = app.Provide(executor.NewPinkGroupManaged)
	// _ = app.Provide(executor.NewPinkExecutor)
	// _ = app.Provide(schedule.NewPinkScheduler)
	// _ = app.Provide(node.NewPinkNode)
	// _ = app.Provide(handler.NewPinkWebHandler)

	_ = app.Provide(api.NewHttpService)

	return app
}

func main() {

	flag.Parse()
	app := BuildApp()

	err := app.Invoke(func(svc *api.Service, config *conf.AppConf, bus *comm.NatsBus) error {
		go bus.Run()

		svc.Run()
		return nil
	})

	if err != nil {
		log.Errorf("the pink service start fail %+v", err)
		panic(err)
	}
}
