package cost

import (
	"reflect"
	"testing"

	"cloud.google.com/go/bigquery"
	"github.com/tidbcloud/cost-collect/pkg/global"
)

func Test_gcpDataExtract(t *testing.T) {
	type args struct {
		row     map[string]bigquery.Value
		request global.Request
	}
	tests := []struct {
		name    string
		args    args
		want    *global.Item
		wantErr bool
	}{
		{
			name: "should work normally",
			args: args{
				row: map[string]bigquery.Value{
					"region":                    bigquery.Value("12345767"),
					"amount":                    bigquery.Value(12345767),
					"unit":                      bigquery.Value("12345767"),
					"service_name":              bigquery.Value("Compute Engine"),
					"description":               bigquery.Value("Network Inter Zone"),
					"cost":                      bigquery.Value(12345767),
					"user_label_tenant_id":      bigquery.Value("12345767"),
					"user_label_project_id":     bigquery.Value("12345767"),
					"user_label_cluster_id":     bigquery.Value("12345767"),
					"user_label_environment_id": bigquery.Value("12345767"),
					"user_label_component_id":   bigquery.Value("12345767"),
					"user_label_usedby":         bigquery.Value("12345767"),
					"user_label_servicetype":    bigquery.Value("12345767"),
				},
				request: global.Request{
					TenantID:    12345767,
					ProjectID:   12345767,
					StartTime:   "2022-10-24 00:00:00",
					EndTime:     "2022-10-25 00:00:00",
					DataVersion: "v2",
				},
			},
			want: &global.Item{
				Labels: global.Labels{
					TenantID:    "12345767",
					ProjectID:   "12345767",
					ClusterID:   "12345767",
					ComponentID: "12345767",
					UsedBy:      "customer",
					ServiceType: "12345767",
					Provider:    "gcp",
				},
				Amount:          12345767,
				Unit:            "12345767",
				Cost:            12345767,
				ResourceType:    "Network",
				ResourceSubtype: "cross_az",
				DataVersion:     "v2",
				ServiceName:     "Compute Engine",
				Description:     "Network Inter Zone",
				Location:        "",
				StartTime:       "2022-10-24 00:00:00",
				EndTime:         "2022-10-25 00:00:00",
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := gcpDataExtract(tt.args.row, tt.args.request)
			if (err != nil) != tt.wantErr {
				t.Errorf("gcpDataExtract() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("gcpDataExtract() = %v, want %v", got, tt.want)
			}
		})
	}
}
