package repository

import (
	"context"
	"strconv"
)

type mockClusterMetaRepo struct {
	ruVersionsByKeyspace map[string]string
}

func NewMockClusterMetaRepo() *mockClusterMetaRepo {
	return &mockClusterMetaRepo{}
}

func NewMockClusterMetaRepoWithRUVersions(versions map[string]string) *mockClusterMetaRepo {
	return &mockClusterMetaRepo{ruVersionsByKeyspace: versions}
}

func (r *mockClusterMetaRepo) buildCluster(primaryID string, clusterID, tenantID, projectID uint64, keyspaceID uint32) *ClusterBranch {
	return &ClusterBranch{
		Type:       CBTypeCluster,
		PrimaryID:  primaryID,
		ClusterID:  clusterID,
		TenantID:   tenantID,
		ProjectID:  projectID,
		KeyspaceID: keyspaceID,
		RUVersion:  r.ruVersionsByKeyspace[strconv.FormatUint(uint64(keyspaceID), 10)],
	}
}

func (r *mockClusterMetaRepo) GetClusterBranchByPrimaryID(ctx context.Context, clusterID string) (*ClusterBranch, error) {
	switch clusterID {
	case "1001":
		return r.buildCluster("1001", 1001, 11, 21, 1), nil
	case "1002":
		return r.buildCluster("1002", 1002, 12, 22, 2), nil
	case "1003":
		return r.buildCluster("1003", 1003, 13, 23, 3), nil
	}
	return nil, nil
}

func (r *mockClusterMetaRepo) GetClusterBranchByKeyspaceID(ctx context.Context, keyspaceID string) (*ClusterBranch, error) {
	switch keyspaceID {
	case "1":
		return r.buildCluster("1001", 1001, 11, 21, 1), nil
	case "2":
		return r.buildCluster("1002", 1002, 12, 22, 2), nil
	case "3":
		return r.buildCluster("1003", 1003, 13, 23, 3), nil
	}
	return nil, nil
}

func (r *mockClusterMetaRepo) ListEssentialPlanClusters(ctx context.Context) ([]*ClusterBranch, error) {
	return []*ClusterBranch{
		{Type: CBTypeCluster, PrimaryID: "1001", ClusterID: 1001, TenantID: 11, ProjectID: 21, KeyspaceID: 1, RUVersion: r.ruVersionsByKeyspace["1"], MaxRcu: 1000, MinRcu: 100},
		{Type: CBTypeCluster, PrimaryID: "1002", ClusterID: 1002, TenantID: 12, ProjectID: 22, KeyspaceID: 2, RUVersion: r.ruVersionsByKeyspace["2"], MaxRcu: 2000, MinRcu: 200},
	}, nil
}
