package main

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/tidbcloud/chaos-svc/idl/pbgen/proto/chaos"
	"github.com/tidbcloud/pkgv2/logid"
	base "go.buf.build/protocolbuffers/go/linuxgit/tidbcloud/base"
	"google.golang.org/grpc"
)

func main() {
	ctx := context.TODO()
	requestid := logid.GenLogid()
	fmt.Println("requstid: ", requestid)
	ctx = logid.WithLogid(ctx, requestid)

	connectTo := "127.0.0.1:20000"
	conn, err := grpc.Dial(connectTo, grpc.WithBlock(), grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	fmt.Println("Connected to", connectTo)
	cli := chaos.NewChaosServiceClient(conn)
	BaseReq := &base.BaseReq{
		Tags: map[string]string{
			logid.GRPCHeaderKey: requestid,
		},
	}
	// var resp interface{}
	// printResp(EnableBackend(ctx, cli, BaseReq))
	// printResp(DisableBackend(ctx, cli, BaseReq))
	// printResp(GetBackend(ctx, cli, BaseReq))
	// printResp(CreateExperiment(ctx, cli, BaseReq))
	// printResp(UpdateExperiment(ctx, cli, BaseReq))
	// printResp(DeleteExperiment(ctx, cli, BaseReq))
	printResp(ListExperiments(ctx, cli, BaseReq))
	// printResp(GetExperiment(ctx, cli, BaseReq))
	// printResp(GetTiDBClusterInfo(ctx, cli, BaseReq))
}

func printResp(resp interface{}, err error) {
	if err != nil {
		fmt.Println(err)
	} else {
		b, _ := json.MarshalIndent(resp, "", "  ")
		fmt.Println(string(b))
	}
}

func EnableBackend(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.EnableChaosBackendRequest{
		ProjectId: 43511,
		ClusterId: 90037,
		UserId:    222,
	}
	return cli.EnableChaosBackend(ctx, req)
}

func DisableBackend(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.DisableChaosBackendRequest{
		ProjectId: 2333,
		ClusterId: 7777,
		UserId:    222,
	}
	return cli.DisableChaosBackend(ctx, req)
}

func GetBackend(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.GetChaosBackendRequest{
		ProjectId: 111,
		ClusterId: 222,
	}
	return cli.GetChaosBackend(ctx, req)
}

func CreateExperiment(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.CreateExperimentRequest{
		BaseReq:   baseReq,
		ProjectId: 2333,
		ClusterId: 7777,
		Draft:     false,
		UserId:    222,
		Action: &chaos.ExperimentAction{
			Type: chaos.ActionKind_ACTION_KIND_POD_SHUTDOWN,
			PodShutdown: &chaos.ExperimentAction_ActionPodShutdown{
				Target: &chaos.ExperimentTarget{
					Mode:      chaos.TargetMode_TARGET_MODE_RANDOM,
					Component: chaos.TargetComponent_TARGET_COMPONENT_TIDB,
				},
			},
		},
	}
	return cli.CreateExperiment(ctx, req)
}

func ListExperiments(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.ListExperimentsRequest{
		BaseReq: baseReq,
		Filter: &chaos.ExperimentFilter{
			ClusterIds: []uint64{7777},
		},
		Pager: &base.Pager{
			Page:     1,
			PageSize: 10,
		},
	}
	return cli.ListExperiments(ctx, req)
}

func UpdateExperiment(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	draft := true
	summary := "test"
	reportLink := "1234"
	req := &chaos.UpdateExperimentRequest{
		BaseReq:      baseReq,
		ProjectId:    2333,
		ClusterId:    7777,
		ExperimentId: 14,
		Draft:        &draft,
		Summary:      &summary,
		ReportLink:   &reportLink,
	}
	return cli.UpdateExperiment(ctx, req)
}

func DeleteExperiment(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.DeleteExperimentRequest{
		BaseReq:      baseReq,
		ProjectId:    2333,
		ClusterId:    7777,
		ExperimentId: 14,
	}
	return cli.DeleteExperiment(ctx, req)
}

func GetExperiment(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.GetExperimentRequest{
		BaseReq:      baseReq,
		ProjectId:    2333,
		ClusterId:    7777,
		ExperimentId: 8,
	}
	return cli.GetExperiment(ctx, req)
}

func GetTiDBClusterInfo(ctx context.Context, cli chaos.ChaosServiceClient, baseReq *base.BaseReq) (interface{}, error) {
	req := &chaos.GetTiDBComponentTopologyRequest{
		BaseReq:   baseReq,
		ProjectId: 73802,
		ClusterId: 90175,
	}
	return cli.GetTiDBComponentTopology(ctx, req)
}
