package migrate

import (
	"github.com/spf13/cobra"

	"cold-delivery/db"
	"gogs.baozhida.cn/zoie/OAuth-core/config/source/file"
	log "gogs.baozhida.cn/zoie/OAuth-core/logger"
	"gogs.baozhida.cn/zoie/OAuth-core/sdk/config"
)

var (
	configYml string
	StartCmd  = &cobra.Command{
		Use:     "migrate",
		Short:   "初始化数据库基础数据",
		Example: "cold-delivery migrate -c conf/settings.yml",
		PreRunE: func(cmd *cobra.Command, args []string) error {
			//1. 读取配置
			config.Setup(
				file.NewSource(file.WithPath(configYml)),
				db.Setup,
			)
			db.AutoMigrateDB()

			return nil
		},
		RunE: func(cmd *cobra.Command, args []string) error {
			if err := InitDb(db.DB); err != nil {
				log.Error("数据库基础数据初始化失败", err.Error())
			} else {
				log.Info("数据库基础数据初始化成功")
			}
			return nil
		},
	}
)

func init() {
	StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
}
