package cost

import (
	"context"
	"fmt"
	"testing"

	"github.com/pingcap/log"
	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/cost-collect/pkg/global"
)

var mg *Manager

type TestA struct {
}

func (t TestA) GetProviderCost(context.Context, global.Request) ([]global.Item, error) {
	ans := []global.Item{{Cost: 3}, {Cost: 8}}
	return ans, nil
}

type TestB struct {
}

func (t TestB) GetProviderCost(ctx context.Context, req global.Request) ([]global.Item, error) {
	if req.TenantID == "" {
		return nil, fmt.Errorf("tennatIDEmpty")
	}
	ans := []global.Item{{Cost: 3}}
	return ans, nil
}

// simple check
func TestCostManagerOK(t *testing.T) {
	mg.Register("testA", TestA{})
	mg.Register("testB", TestB{})

	list, err := mg.QueryBill(context.Background(), global.Request{TenantID: "test"})
	assert.NoError(t, err)
	if len(list) != 3 {
		panic(fmt.Errorf("answer %d not right,it should be %d", len(list), 3))
	}
}

// check if one plugin error
func TestCostManagerError(t *testing.T) {
	mg.Register("testA", TestA{})
	mg.Register("testB", TestB{})

	list, err := mg.QueryBill(context.Background(), global.Request{})
	assert.NotNil(t, err)
	if len(list) != 0 {
		panic(fmt.Errorf("answer %d not right,it should be %d", len(list), 0))
	}
	log.S().Infof("%s", err)
}

func init() {
	mg = NewManager()
}
