//go:generate ${GOBIN}/mockgen -destination bigquery_cli_mock.go -package=googlecli ${GO_MODULE}/pkg/infra/clients/googlecli BigQueryCli
package googlecli

import (
	"context"
	"fmt"

	"cloud.google.com/go/bigquery"
	"golang.org/x/oauth2/google"
	"google.golang.org/api/option"
)

type BigQueryCli interface {
	Query(ctx context.Context, q string) (*bigquery.RowIterator, error)
}

type bigQueryClient struct {
	bigQueryCli *bigquery.Client
}

func NewBigQueryClientFromCred(ctx context.Context, cred *google.Credentials, projectID string) (BigQueryCli, error) {
	bigQueryCli, err := bigquery.NewClient(ctx, projectID, option.WithCredentials(cred))
	if err != nil {
		return nil, fmt.Errorf("failed to get bigquery client: %w", err)
	}
	return &bigQueryClient{bigQueryCli: bigQueryCli}, nil
}

func (b *bigQueryClient) Query(ctx context.Context, q string) (*bigquery.RowIterator, error) {
	query := b.bigQueryCli.Query(q)
	return query.Read(ctx)
}
