// Package port defines interfaces (ports) for external dependencies.
package port

import (
	"context"
	"time"

	"github.com/orchard9/rdev/internal/domain"
)

// GitRepository manages git repositories via external git server (Gitea).
type GitRepository interface {
	// CreateRepo creates a new git repository.
	CreateRepo(ctx context.Context, name, description string, private bool) (*domain.Repo, error)

	// DeleteRepo deletes a repository.
	DeleteRepo(ctx context.Context, owner, name string) error

	// ListRepos returns all repositories for an owner.
	ListRepos(ctx context.Context, owner string) ([]*domain.Repo, error)

	// GetRepo returns a single repository.
	GetRepo(ctx context.Context, owner, name string) (*domain.Repo, error)

	// AddCollaborator adds a user as collaborator to a repo.
	// permission can be "read", "write", or "admin".
	AddCollaborator(ctx context.Context, owner, repo, username string, permission string) error

	// RemoveCollaborator removes a collaborator from a repo.
	RemoveCollaborator(ctx context.Context, owner, repo, username string) error

	// AddDeployKey adds a deploy key to a repo for read-only or read-write access.
	AddDeployKey(ctx context.Context, owner, repo, title, publicKey string, readOnly bool) (*domain.DeployKey, error)

	// DeleteDeployKey removes a deploy key from a repo.
	DeleteDeployKey(ctx context.Context, owner, repo string, keyID int64) error

	// CreateWebhook creates a webhook to trigger on specified events.
	CreateWebhook(ctx context.Context, owner, repo, url, secret string, events []string) (*domain.RepoWebhook, error)

	// DeleteWebhook removes a webhook from a repo.
	DeleteWebhook(ctx context.Context, owner, repo string, webhookID int64) error

	// ListBranches returns all branches for a repository.
	ListBranches(ctx context.Context, owner, repo string) ([]*domain.GitBranch, error)

	// CreateBranch creates a new branch from a reference (branch name or commit SHA).
	CreateBranch(ctx context.Context, owner, repo, branchName, fromRef string) (*domain.GitBranch, error)

	// CreateAccessToken creates a new personal access token for git operations.
	// Returns the token value (only available once), token ID, and any error.
	CreateAccessToken(ctx context.Context, name string, scopes []string, expiresAt *time.Time) (*domain.GitAccessToken, error)

	// DeleteAccessToken revokes and deletes an access token.
	DeleteAccessToken(ctx context.Context, tokenID int64) error
}
