package tioperator

import (
	"context"

	"github.com/tidbcloud/cluster-service-ng/domain/entity"
	"sigs.k8s.io/controller-runtime/pkg/client"
)

func NewTiOperator(k8sClient client.Client) TiOperator {
	return &tiOperatorImpl{
		client: k8sClient,
	}
}

type tiOperatorImpl struct {
	client client.Client
}

func (t *tiOperatorImpl) ListClusters(ctx context.Context) ([]*entity.SharedPool, error) {
	return ListClusters(ctx, t.client)
}

func (t *tiOperatorImpl) GetCluster(ctx context.Context, clusterId string) (*entity.SharedPool, error) {
	return GetCluster(ctx, t.client, clusterId)
}

// tidb group
func (t *tiOperatorImpl) ListTiDBGroups(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiDBGroups(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiDBGroup(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiDBGroup(ctx, t.client, clusterId, groupId)
}

//	func (t *tiOperatorImpl) GetTiDBAuditLogConfig(ctx context.Context, clusterId, groupId string) (*entity.AuditLogConfig, error) {
//		return GetTiDBAuditLogConfig(ctx, t.client, clusterId, groupId)
//	}
func (t *tiOperatorImpl) UpdateTiDBGroup(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiDBGroup(ctx, t.client, clusterId, groupId, options)
}

// tidb worker
func (t *tiOperatorImpl) ListTiDBWorkers(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiDBWorkers(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiDBWorker(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiDBWorker(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTiDBWorker(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiDBWorker(ctx, t.client, clusterId, groupId, options)
}

// tikv group
func (t *tiOperatorImpl) ListTiKVGroups(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiKVGroups(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiKVGroup(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiKVGroup(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTiKVGroup(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiKVGroup(ctx, t.client, clusterId, groupId, options)
}
func (t *tiOperatorImpl) CreatePlacementPolicy(ctx context.Context, sharedPoolId string, policy *entity.PlacementPolicy, keyspaceId uint32) (*entity.PlacementPolicy, error) {
	return CreatePlacementPolicy(ctx, t.client, sharedPoolId, policy, keyspaceId)
}
func (t *tiOperatorImpl) ListPlacementPolicies(ctx context.Context, sharedPoolId, tikvGroupId string) ([]*entity.PlacementPolicy, error) {
	return ListPlacementPolicies(ctx, t.client, sharedPoolId, tikvGroupId)
}
func (t *tiOperatorImpl) GetPlacementPolicy(ctx context.Context, sharedPoolId, placementPolicyId string) (*entity.PlacementPolicy, error) {
	return GetPlacementPolicy(ctx, t.client, sharedPoolId, placementPolicyId)
}
func (t *tiOperatorImpl) UpdatePlacementPolicy(ctx context.Context, sharedPoolId string, policy *entity.PlacementPolicy, keyspaceId uint32) (*entity.PlacementPolicy, error) {
	return UpdatePlacementPolicy(ctx, t.client, sharedPoolId, policy, keyspaceId)
}
func (t *tiOperatorImpl) DeletePlacementPolicy(ctx context.Context, sharedPoolId, placementPolicyId string) error {
	return DeletePlacementPolicy(ctx, t.client, sharedPoolId, placementPolicyId)
}

// tikv worker
func (t *tiOperatorImpl) ListTiKVWorkers(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiKVWorkers(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiKVWorker(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiKVWorker(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTiKVWorker(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiKVWorker(ctx, t.client, clusterId, groupId, options)
}

// coprocessor worker
func (t *tiOperatorImpl) ListCoprocessorWorkers(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListCoprocessorWorkers(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetCoprocessorWorker(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetCoprocessorWorker(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateCoprocessorWorker(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateCoprocessorWorker(ctx, t.client, clusterId, groupId, options)
}

// schema manager
func (t *tiOperatorImpl) ListSchemaManagers(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListSchemaManagers(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetSchemaManager(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetSchemaManager(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateSchemaManager(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateSchemaManager(ctx, t.client, clusterId, groupId, options)
}

// tiflash CN
func (t *tiOperatorImpl) ListTiFlashCNs(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiFlashCNs(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiFlashCN(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiFlashCN(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTiFlashCN(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiFlashCN(ctx, t.client, clusterId, groupId, options)
}

// tiflash WN
func (t *tiOperatorImpl) ListTiFlashWNs(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiFlashWNs(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiFlashWN(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiFlashWN(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTiFlashWN(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiFlashWN(ctx, t.client, clusterId, groupId, options)
}

// pd group
func (t *tiOperatorImpl) ListPDGroups(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListPDGroups(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetPDGroup(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetPDGroup(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdatePDGroup(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdatePDGroup(ctx, t.client, clusterId, groupId, options)
}

// tso group
func (t *tiOperatorImpl) ListTsoGroups(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTsoGroups(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTsoGroup(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTsoGroup(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTsoGroup(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTsoGroup(ctx, t.client, clusterId, groupId, options)
}

// scheduling group
func (t *tiOperatorImpl) ListSchedulingGroups(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListSchedulingGroups(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetSchedulingGroup(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetSchedulingGroup(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateSchedulingGroup(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateSchedulingGroup(ctx, t.client, clusterId, groupId, options)
}

// tiproxy group
func (t *tiOperatorImpl) ListTiProxyGroups(ctx context.Context, clusterId string) ([]*entity.Component, error) {
	return ListTiProxyGroups(ctx, t.client, clusterId)
}
func (t *tiOperatorImpl) GetTiProxyGroup(ctx context.Context, clusterId, groupId string) (*entity.Component, error) {
	return GetTiProxyGroup(ctx, t.client, clusterId, groupId)
}
func (t *tiOperatorImpl) UpdateTiProxyGroup(ctx context.Context, clusterId, groupId string, options ...UpdateOption) (*entity.Component, error) {
	return UpdateTiProxyGroup(ctx, t.client, clusterId, groupId, options)
}
