package client

import (
	"context"

	bg "cloud.google.com/go/bigquery"
	bgapi "cloud.google.com/go/bigquery/storage/apiv1"
	"github.com/pingcap/errors"
	"github.com/tidbcloud/ecosystem-service/config"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/option"
)

const (
	ProjectID = "gcp-digital-bi"
)

type Client struct {
	*bg.Client
	Ctx context.Context
}

func NewBigqueryClient(conf *config.ConfigWrapper) (*Client, error) {
	ctx := context.Background()

	creds, err := google.CredentialsFromJSON(ctx, []byte(conf.BigQueryConfig.Credential), bgapi.DefaultAuthScopes()...)
	if err != nil {
		return nil, errors.Trace(err)
	}
	client, err := bg.NewClient(ctx, ProjectID, option.WithCredentials(creds))
	if err != nil {
		return nil, errors.Trace(err)
	}
	return &Client{
		Client: client,
		Ctx:    ctx,
	}, nil
}
