package cmd

import (
	"encoding/json"
	bootstarpSerer "github.com/tidbcloud/dataflow-service/internal/cmd/bootstrap/server"

	"github.com/spf13/cobra"
	"github.com/tidbcloud/dataflow-service/app/container"
	"github.com/tidbcloud/dataflow-service/app/models/domain"
	"github.com/tidbcloud/dataflow-service/pkg/log"
	"go.uber.org/zap"
)

func registerRestoreJobFunc(cmd *cobra.Command) {
	{
		var createReqString string
		suBcmd := &cobra.Command{
			Use:   "test-restore-backup",
			Short: "restore backup",
			RunE: func(cmd *cobra.Command, args []string) error {
				if _, err := bootstarpSerer.NewServer(cmd.Context(), bootstarpSerer.ServerOption{
					EnableEventBusConsumer: false,
				}); err != nil {
					return err
				}
				ctx := log.WithTraceID(cmd.Context())
				app := container.Global()
				createReq := &domain.CreateRestoreReqV2{}
				err := json.Unmarshal([]byte(createReqString), createReq)
				if err != nil {
					log.GetLogger(ctx).Error("unmarshal createReqString", zap.Error(err))
					return err
				}
				resp, err := app.RestoreModelRouter.Create(ctx, createReq)
				if err != nil {
					log.GetLogger(ctx).Error("create snapshot restore", zap.Error(err))
					return err
				}
				log.GetLogger(ctx).Info("create snapshot success", zap.Any("resp", resp))
				return nil
			},
		}
		suBcmd.Flags().StringVarP(&createReqString, "request", "", "", "")
		cmd.AddCommand(suBcmd)
	}
	{
		var restoreID uint64
		suBcmd := &cobra.Command{
			Use:   "test-recycle-restore",
			Short: "recycle restore",
			RunE: func(cmd *cobra.Command, args []string) error {
				if _, err := bootstarpSerer.NewServer(cmd.Context(), bootstarpSerer.ServerOption{
					EnableEventBusConsumer: false,
				}); err != nil {
					return err
				}
				ctx := log.WithTraceID(cmd.Context())
				app := container.Global()
				resp, err := app.RestoreModelRouter.Cancel(ctx, &domain.CancelRestoreReqV2{
					RestoreID: restoreID,
				})
				if err != nil {
					log.GetLogger(ctx).Error("delete snapshot restore", zap.Error(err))
					return err
				}
				log.GetLogger(ctx).Info("delete snapshot success", zap.Any("resp", resp))
				return nil
			},
		}
		suBcmd.Flags().Uint64VarP(&restoreID, "restoreID", "", 0, "")
		cmd.AddCommand(suBcmd)
	}
	{
		var listReqString string
		suBcmd := &cobra.Command{
			Use:   "list-restores",
			Short: "list restores",
			RunE: func(cmd *cobra.Command, args []string) error {
				if _, err := bootstarpSerer.NewServer(cmd.Context(), bootstarpSerer.ServerOption{
					EnableEventBusConsumer: false,
				}); err != nil {
					return err
				}
				ctx := log.WithTraceID(cmd.Context())
				app := container.Global()
				listReq := &domain.ListRestoreReqV2{}
				err := json.Unmarshal([]byte(listReqString), listReq)
				if err != nil {
					log.GetLogger(ctx).Error("unmarshal listReqString", zap.Error(err))
					return err
				}
				resp, err := app.RestoreSnapShotModel.List(ctx, listReq)
				if err != nil {
					log.GetLogger(ctx).Error("list restore", zap.Error(err))
					return err
				}
				log.GetLogger(ctx).Info("list restores success", zap.Any("resp", resp))
				return nil
			},
		}
		suBcmd.Flags().StringVarP(&listReqString, "request", "", "", "")
		cmd.AddCommand(suBcmd)
	}
}

func init() {
	restoreCmd := &cobra.Command{Use: "restore", Short: "restore cmd"}
	restoreCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
		return nil
	}

	registerRestoreJobFunc(restoreCmd)
	jobCmd.AddCommand(restoreCmd)
}
