package cost

import (
	"context"
	"testing"
	"text/template"

	"github.com/andreyvit/diff"
	"github.com/tidbcloud/cost-collect/pkg/cloud/gcp"
	"github.com/tidbcloud/cost-collect/pkg/global"
)

func TestCreateBigquerySQL(t *testing.T) {
	type args struct {
		tpl  *template.Template
		args gcp.BigQueryParam
	}
	tests := []struct {
		name    string
		args    args
		want    string
		wantErr bool
	}{
		{
			name: "GCP SQL without projectID and tenantID",
			args: args{
				tpl: func() *template.Template {
					tpl, err := GetGCPSQLTemplate()
					if err != nil {
						t.Fatal(err)
					}
					return tpl
				}(),
				args: gcp.BigQueryParam{
					Request: global.Request{
						TenantID:    0,
						ProjectID:   0,
						StartTime:   "2022-10-24 00:00:00",
						EndTime:     "2022-10-25 00:00:00",
						DataVersion: "v2",
					},
					Config: gcp.Config{
						PreAggregatedGCPTable: "dbaas-prod.gcp_prod_billing.gcp_traffic_billing_daily",
					},
				},
			},
			want: `
SELECT
    region,amount,unit,description,service_name,cost,
    user_label_tenant_id,user_label_project_id,user_label_cluster_id,user_label_environment_id,user_label_component_id,
    user_label_usedby,user_label_servicetype
FROM ` + "`" + `dbaas-prod.gcp_prod_billing.gcp_traffic_billing_daily` + "`" + `
WHERE
	usage_start_date >= DATE("2022-10-24 00:00:00") AND usage_start_date < DATE("2022-10-25 00:00:00")
    
    
ORDER BY user_label_tenant_id,user_label_project_id
;`,
			wantErr: false,
		},
		{
			name: "GCP SQL",
			args: args{
				tpl: func() *template.Template {
					tpl, err := GetGCPSQLTemplate()
					if err != nil {
						t.Fatal(err)
					}
					return tpl
				}(),
				args: gcp.BigQueryParam{
					Request: global.Request{
						TenantID:    123456,
						ProjectID:   1234567,
						StartTime:   "2022-10-24 00:00:00",
						EndTime:     "2022-10-25 00:00:00",
						DataVersion: "v2",
					},
					Config: gcp.Config{
						PreAggregatedGCPTable: "dbaas-prod.gcp_prod_billing.gcp_traffic_billing_daily",
					},
				},
			},
			want: `
SELECT
    region,amount,unit,description,service_name,cost,
    user_label_tenant_id,user_label_project_id,user_label_cluster_id,user_label_environment_id,user_label_component_id,
    user_label_usedby,user_label_servicetype
FROM ` + "`" + `dbaas-prod.gcp_prod_billing.gcp_traffic_billing_daily` + "`" + `
WHERE
	usage_start_date >= DATE("2022-10-24 00:00:00") AND usage_start_date < DATE("2022-10-25 00:00:00")
    AND user_label_tenant_id = "123456"
    AND user_label_project_id = "1234567"
ORDER BY user_label_tenant_id,user_label_project_id
;`,
			wantErr: false,
		},
		{
			name: "AWS SQL without projectID and tenantID",
			args: args{
				tpl: func() *template.Template {
					tpl, err := GetAWSSQLTemplate()
					if err != nil {
						t.Fatal(err)
					}
					return tpl
				}(),
				args: gcp.BigQueryParam{
					Request: global.Request{
						TenantID:    0,
						ProjectID:   0,
						StartTime:   "2022-10-24 00:00:00",
						EndTime:     "2022-10-25 00:00:00",
						DataVersion: "v2",
					},
					Config: gcp.Config{
						PreAggregatedAWSTable: "dbaas-prod.aws_prod_billing.aws_traffic_billing_daily",
					},
				},
			},
			want: `
SELECT resource_tags_user_tenant,resource_tags_user_project,resource_tags_user_usedby,
    line_item_blended_cost,line_item_unblended_cost,line_item_usage_amount,pricing_unit,
    service_name,line_item_operation,product_region_code,
    resource_tags_user_cluster,resource_tags_user_component,resource_tags_user_environment,resource_tags_user_servicetype
FROM ` + "`" + `dbaas-prod.aws_prod_billing.aws_traffic_billing_daily` + "`" + `
WHERE 
	line_item_usage_start_date >= DATE("2022-10-24 00:00:00") AND line_item_usage_start_date < DATE("2022-10-25 00:00:00")
    
    
ORDER BY resource_tags_user_tenant,resource_tags_user_project
`,
			wantErr: false,
		},
		{
			name: "AWS SQL",
			args: args{
				tpl: func() *template.Template {
					tpl, err := GetAWSSQLTemplate()
					if err != nil {
						t.Fatal(err)
					}
					return tpl
				}(),
				args: gcp.BigQueryParam{
					Request: global.Request{
						TenantID:    123456,
						ProjectID:   1234567,
						StartTime:   "2022-10-24 00:00:00",
						EndTime:     "2022-10-25 00:00:00",
						DataVersion: "v2",
					},
					Config: gcp.Config{
						PreAggregatedAWSTable: "dbaas-prod.aws_prod_billing.aws_traffic_billing_daily",
					},
				},
			},
			want: `
SELECT resource_tags_user_tenant,resource_tags_user_project,resource_tags_user_usedby,
    line_item_blended_cost,line_item_unblended_cost,line_item_usage_amount,pricing_unit,
    service_name,line_item_operation,product_region_code,
    resource_tags_user_cluster,resource_tags_user_component,resource_tags_user_environment,resource_tags_user_servicetype
FROM ` + "`" + `dbaas-prod.aws_prod_billing.aws_traffic_billing_daily` + "`" + `
WHERE 
	line_item_usage_start_date >= DATE("2022-10-24 00:00:00") AND line_item_usage_start_date < DATE("2022-10-25 00:00:00")
    AND resource_tags_user_tenant = '123456'
    AND resource_tags_user_project = '1234567'
ORDER BY resource_tags_user_tenant,resource_tags_user_project
`,
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got, err := CreateBigquerySQL(context.Background(), tt.args.tpl, tt.args.args)
			if (err != nil) != tt.wantErr {
				t.Errorf("CreateBigquerySQL() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("CreateBigquerySQL() = %v, want %v, diff %v", got, tt.want, diff.LineDiff(got, tt.want))
			}
		})
	}
}
