package common

import (
	"context"
	"testing"

	"github.com/tidbcloud/infra-provider/regional-server/api/v1alpha1"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/common/o11ycli/api"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/config"
	"github.com/tidbcloud/infra-provider/regional-server/pkg/controllers/o11yroute/task"
)

func TestClusterUsesConfiguredBizTypeForCreateAndDelete(t *testing.T) {
	t.Parallel()

	o11yClient := &recordingO11yClient{}
	cluster := NewCluster(&task.Context{
		APPBox: &v1alpha1.APPBox{
			Spec: v1alpha1.APPBoxSpec{Region: "us-west-2"},
		},
		O11yRoute: &v1alpha1.O11yRoute{
			Spec: v1alpha1.O11yRouteSpec{ApplicationID: api.ApplicationIDInfra},
		},
		Config: config.RootConfig{
			ControllerConfig: config.ControllerConfig{Env: "dev"},
			O11yBizType:      "cp9-session-1",
		},
		O11yClient: o11yClient,
	})

	cluster.Create(context.Background())
	if o11yClient.createRequest == nil {
		t.Fatal("create request was not sent")
	}
	if got := o11yClient.createRequest.BizType; got != "cp9-session-1" {
		t.Fatalf("create bizType = %q, want %q", got, "cp9-session-1")
	}

	cluster.Delete(context.Background())
	if o11yClient.listRequest == nil {
		t.Fatal("list request was not sent during delete")
	}
	if got := o11yClient.listRequest.BizType; got != "cp9-session-1" {
		t.Fatalf("delete lookup bizType = %q, want %q", got, "cp9-session-1")
	}
}

type recordingO11yClient struct {
	createRequest *api.CreateRequest
	listRequest   *api.ListRequest
}

func (c *recordingO11yClient) CreateCluster(_ context.Context, req *api.CreateRequest) (*api.ObservabilityCluster, error) {
	c.createRequest = req
	return &api.ObservabilityCluster{
		ClusterBaseInfo: api.ClusterBaseInfo{ID: "o11y-cluster-1"},
	}, nil
}

func (c *recordingO11yClient) ListClusters(_ context.Context, req *api.ListRequest) ([]api.ClusterBaseInfo, error) {
	c.listRequest = req
	return nil, nil
}

func (*recordingO11yClient) GetCluster(context.Context, string, string, string) (*api.ObservabilityCluster, error) {
	return nil, nil
}

func (*recordingO11yClient) DeleteCluster(context.Context, string, string, string) error {
	return nil
}

func (*recordingO11yClient) PauseCluster(context.Context, string, string, string) error {
	return nil
}

func (*recordingO11yClient) ResumeCluster(context.Context, string, string, string) error {
	return nil
}

func (*recordingO11yClient) UpdateClusterAgent(context.Context, *api.UpdateAgentRequest) error {
	return nil
}
