/*
 * Copyright 2021 PingCAP, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"context"
	"fmt"

	"github.com/spf13/cobra"

	"github.com/pingcap/mimir/pkg/protos"
	"github.com/pingcap/mimir/pkg/tui"
)

func newCreateSinkCmd() *cobra.Command {
	tpy := "tidb"
	table := ""
	db := "mimir"
	schedule := ""

	cmd := &cobra.Command{
		Use:   `create-sink <sink-name>`,
		Short: `create a new sink`,
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return cmd.Help()
			}

			if tpy == "tidb" && table == "" {
				return fmt.Errorf("table must be specified when the type is tidb")
			}
			ctx := context.Background()

			client, err := newClient()
			if err != nil {
				return err
			}

			sink, err := client.CreateSink(ctx, &protos.CreateSinkParam{
				SinkID:   args[0],
				Type:     tpy,
				Table:    table,
				Database: db,
				Schedule: schedule,
			})
			if err != nil {
				return fmt.Errorf("create sink failed: %s", err)
			}
			fmt.Printf("create sink success, id: %s\n", sink.SinkID)
			return nil
		},
	}

	cmd.Flags().StringVarP(&tpy, "type", "", tpy, "the sink type, at this time, only tidb is supported")
	cmd.Flags().StringVarP(&table, "table", "", table, "the table name binding to this sink (when the type is tidb)")
	cmd.Flags().StringVarP(&db, "db", "", db, "the database binding to this sink (when the type is tidb)")
	cmd.Flags().StringVarP(&schedule, "schedule", "", schedule, "how often to check if the sink is updated")

	return cmd
}

func newListSinkCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   `list-sink`,
		Short: `display sink list`,
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := context.Background()

			client, err := newClient()
			if err != nil {
				return err
			}

			data, err := client.ListSink(ctx)
			if err != nil {
				return fmt.Errorf("list sink failed: %s", err)
			}

			var sinkTable [][]string
			sinkTable = append(sinkTable, []string{"Sink ID", "Type", "table"})
			for _, sink := range data.Items {
				sinkTable = append(sinkTable, []string{sink.SinkID, sink.SinkType, sink.Table})
			}
			tui.PrintTable(sinkTable, true)

			return nil
		},
	}

	return cmd
}

func newRmSinkCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:  `rm-sink`,
		Long: `rm sink from sink list`,
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) != 1 {
				return cmd.Help()
			}
			ctx := context.Background()
			client, err := newClient()
			if err != nil {
				return err
			}
			data, err := client.RmSink(
				ctx,
				&protos.RmSinkParam{
					SinkID: args[0],
				},
			)
			if err != nil {
				return fmt.Errorf("rm sink failed: %s", err)
			}
			fmt.Printf("Rm sink %s success\n", data.SinkID)
			return nil
		},
	}
	return cmd
}
