package service

import (
	"context"
	"fmt"
	"testing"

	"cloud.google.com/go/bigquery"
)

func TestFetchMarchRevenueOrderData(t *testing.T) {
	ctx := context.Background()

	bqSrv, err := bigquery.NewClient(ctx, GcpProject)
	if err != nil {
		t.Fatalf("Failed to create bigquery client: %v", err)
	}
	defer bqSrv.Close()

	billMonth := "2026-03"

	results, err := fetchRevenueOrderData(ctx, bqSrv, billMonth)
	if err != nil {
		t.Fatalf("fetchRevenueOrderData failed: %v", err)
	}

	t.Logf("Fetched %d records for %s", len(results), billMonth)

	for i, r := range results {
		if i >= 5 {
			t.Logf("... (showing first 5 only)")
			break
		}
		t.Logf("[%d] tenant_id=%s account_surrogate_key=%s account_region=%s payment_type=%s",
			i,
			r.TenantID.StringVal,
			r.AccountSurrogateKey.StringVal,
			r.AccountRegion.StringVal,
			r.PaymentType.StringVal,
		)
	}

	// 验证字段非全空
	nonEmptyPaymentType := 0
	for _, r := range results {
		if r.PaymentType.Valid && r.PaymentType.StringVal != "" {
			nonEmptyPaymentType++
		}
	}
	fmt.Printf("Records with non-empty payment_type: %d / %d\n", nonEmptyPaymentType, len(results))
}
