//go:generate ${GOBIN}/mockgen -destination client_mock.go -package=fadcli ${GO_MODULE}/pkg/infra/clients/fadcli Client
package fadcli

import (
	"fmt"

	relent "github.com/tidbcloud/devops/entity/release"
	"github.com/tidbcloud/devops/pkg/infra/config"
	"github.com/tidbcloud/devops/pkg/infra/consts"
)

type Client interface {
	MarkPR(pr *relent.GithubPR) error
	DeployNewRegion(req *DeployRegionReq) error
	DeployNewBYOCRegion(req *DeployBYOCRegionReq) error
	DeployNewNextGenRegion(req *DeployNextGenRegionReq) error
}

type client struct {
	Env          string
	DSPublicKey  string
	DSPrivateKey string
	GithubToken  string
}

func NewClient(conf *config.RootConfig) (Client, error) {
	return &client{
		Env:          string(consts.GetEnv()),
		DSPublicKey:  conf.Clients.FAD.DataServicePublicKey,
		DSPrivateKey: conf.Clients.FAD.DataServicePrivateKey,
		GithubToken:  conf.Clients.Github.Token,
	}, nil
}

func (c *client) MarkPR(pr *relent.GithubPR) error {
	if err := c.storePRLink(pr.Link); err != nil {
		return err
	}

	return c.rerunCheckForPR(pr)
}

func (c *client) DeployNewRegion(req *DeployRegionReq) error {
	if req.TiDBCloudInstance == "prod" && c.Env != "prod" {
		return fmt.Errorf("can't deploy prod region in ops %s env", c.Env)
	}
	return c.insertRegionCreateRequest(req)
}

func (c *client) DeployNewBYOCRegion(req *DeployBYOCRegionReq) error {
	if req.TiDBCloudInstance == "prod" && c.Env != "prod" {
		return fmt.Errorf("can't deploy prod region in ops %s env", c.Env)
	}
	return c.insertBYOCRegionCreateRequest(req)
}

func (c *client) DeployNewNextGenRegion(req *DeployNextGenRegionReq) error {
	if req.TiDBCloudInstance == "prod" && c.Env != "prod" {
		return fmt.Errorf("can't deploy prod region in ops %s env", c.Env)
	}
	return c.insertNextGenRegionCreateRequest(req)
}
