//go:generate ${GOBIN}/dummygen -d=api_dummy.go -package=github ${GO_MODULE}/pkg/service/github GithubServiceAPI
//go:generate ${GOBIN}/mockgen -destination github_service_mock.go -package=github ${GO_MODULE}/pkg/service/github GithubServiceAPI
package github

import (
	"context"

	ent "github.com/tidbcloud/devops/entity/github"
	"github.com/tidbcloud/devops/pkg/infra/clients/githubcli"
)

type ListRepositoryParam struct {
	Owners       []string
	LocationLike string

	Page    int
	PerPage int
}

func (l ListRepositoryParam) Normalize() (ListRepositoryParam, error) {
	if l.Page < 0 {
		l.Page = 1
	}
	if l.PerPage < 0 {
		l.PerPage = 20
	}

	return l, nil
}

type ListPullRequestsParam struct {
	RepoLocation *ent.RepoLocation
	PRLocations  []ent.PRLocation

	Page    int // 0 means no paging
	PerPage int
}

func (l ListPullRequestsParam) Normalize() ListPullRequestsParam {
	if l.Page < 0 {
		l.Page = 1
	}
	if l.PerPage < 0 {
		l.PerPage = 20
	}

	return l
}

type GithubServiceAPI interface {
	// RawAPI return a github client who access github directly.
	RawAPI() *githubcli.GithubClient

	// WatchPullRequest save pull requests to our DB.
	WatchPullRequest(ctx context.Context, locations []ent.PRLocation) error
	// ListPullRequests list pull requests from  DB.
	ListPullRequests(ctx context.Context, param ListPullRequestsParam) ([]*ent.PullRequest, int64, error)
	// GetPullRequestByLoc get a pull request by location.
	GetPullRequestByLoc(ctx context.Context, loc ent.PRLocation) (*ent.PullRequest, error)
	// SyncPullRequests sync all watched pull requests.
	SyncPullRequests(ctx context.Context) error

	// ListRepository list repositories.
	ListRepository(ctx context.Context, param ListRepositoryParam) ([]*ent.Repository, int64, error)
	// GetRepositoryByLoc get a repository by location.
	GetRepositoryByLoc(ctx context.Context, loc ent.RepoLocation) (*ent.Repository, error)
	// SyncRepository sync all repositories.
	SyncRepository(ctx context.Context) error
}
