package app

import (
	"context"

	"github.com/tidbcloud/AgentTeam/internal/store/app/webhooks"
	"github.com/tidbcloud/AgentTeam/internal/store/provider"
)

func (store *Store) ResolveProviderWatchConnection(ctx context.Context, providerName string, workspaceID string, repositoryFullName string, expectedConnectionRef string) (string, provider.ProviderWatchConnectionStatus, error) {
	connectionRef, status, err := store.resolvePullRequestSubscriptionConnection(ctx, providerName, workspaceID, repositoryFullName, expectedConnectionRef)
	return connectionRef, providerWatchConnectionStatusFromServiceEvent(status), err
}

func providerWatchConnectionStatusFromServiceEvent(status webhooks.ServiceEventConnectionStatus) provider.ProviderWatchConnectionStatus {
	switch status {
	case webhooks.ServiceEventConnectionReady:
		return provider.ProviderWatchConnectionReady
	case webhooks.ServiceEventConnectionWorkspaceNotConfigured:
		return provider.ProviderWatchConnectionWorkspaceNotConfigured
	case webhooks.ServiceEventConnectionResourceNotAccessible:
		return provider.ProviderWatchConnectionResourceNotAccessible
	default:
		return provider.ProviderWatchConnectionWorkspaceNotConfigured
	}
}
