package repos

import (
	"context"
	"time"
)

// AgentOwnership is the minimal service identity of an agent owned by one principal; it is not a AgentTeam agent store model or API DTO.
type AgentOwnership struct {
	ID          string
	Name        string
	DisplayName string
}

// AgentOwnershipSource is the domain adapter port for listing agents already authorized for repository ownership decisions.
type AgentOwnershipSource interface {
	ListOwnedAgents(ctx context.Context, workspaceID, subject string) ([]AgentOwnership, error)
}

// AgentOwnershipSourceFunc adapts existing domain ownership providers to the
// shared repository helper contract. It keeps each resource domain free to own
// its public ownership type while Skill can depend only on this small shape.
type AgentOwnershipSourceFunc func(ctx context.Context, workspaceID, subject string) ([]AgentOwnership, error)

// ListOwnedAgents delegates to the configured ownership source. A nil adapter
// exposes no agents rather than granting repository access.
func (f AgentOwnershipSourceFunc) ListOwnedAgents(ctx context.Context, workspaceID, subject string) ([]AgentOwnership, error) {
	if f == nil {
		return nil, nil
	}
	return f(ctx, workspaceID, subject)
}

const (
	RepositorySyncTokenDefaultProviderHost = "github.com"
	RepositorySyncTokenScopeRepositories   = "repositories"
	RepositorySyncCapabilityRead           = "repo:read"
	RepositorySyncCapabilityClone          = "repo:clone"
	RepositorySyncCapabilityWrite          = "repo:write"
)

// RepositorySyncTokenRequest is the service-to-service request for a bounded repository sync credential, not a persisted token or API request DTO.
type RepositorySyncTokenRequest struct {
	WorkspaceID      string
	ActorPrincipalID string
	ProviderHost     string
	Repositories     []string
	Capabilities     []string
	DisplayName      string
	ExpiresAt        *time.Time
}

// RepositorySyncTokenLease carries the short-lived plaintext credential returned once by its issuer; it is never a persistence model.
type RepositorySyncTokenLease struct {
	Token     string
	ExpiresAt time.Time
}

// RepositorySyncTokenIssuer is the narrow credential-issuance port consumed by resource lifecycle services.
type RepositorySyncTokenIssuer interface {
	CreateRepositorySyncToken(context.Context, RepositorySyncTokenRequest) (RepositorySyncTokenLease, error)
}
