package larkbot

import (
	"github.com/pingcap-inc/pinsights/internal/larkbot/config"
	"github.com/pingcap-inc/pinsights/internal/larkbot/options"
	"github.com/pingcap-inc/pinsights/pkg/app"
	"github.com/pingcap-inc/pinsights/pkg/log"
)

const commandDesc = `The Pinsights larkbot server is the service for Pinsights app in lark. It can receive lark event and .`

func NewApp(bname string) *app.App {
	opts := options.NewOptions()
	application := app.NewApp("Pinsights LarkBot Server",
		bname,
		app.WithOptions(opts),
		app.WithDescription(commandDesc),
		app.WithDefaultValidArgs(),
		app.WithRunFunc(run(opts)),
	)
	return application
}

func run(opts *options.Options) app.RunFunc {
	return func(bname string) error {
		log.Init(opts.Log)
		defer log.Flush()

		cfg, err := config.CreateConfigFromOptions(opts)
		if err != nil {
			return err
		}

		return Run(cfg)
	}
}
