package serviceaccount

import (
	"context"

	serviceaccountstore "github.com/tidbcloud/AgentTeam/internal/store/serviceaccount"
)

// LocalAccountDeletionObserver applies workspace-owner-local effects after a
// workspace-local Service Account has been deleted. Implementations must be
// idempotent: workspace-owned deletion invokes the observer as a post-commit
// fast path, while platform-account deletion may deliver the same tombstone
// again from its durable cross-profile worker after lease expiry or a transient
// observer failure.
type LocalAccountDeletionObserver interface {
	LocalAccountsDeleted(context.Context, []serviceaccountstore.Account) error
}

// SetLocalAccountDeletionObserver installs the post-commit observer used by
// workspace-owned Service Account deletion.
func (service *Service) SetLocalAccountDeletionObserver(observer LocalAccountDeletionObserver) {
	if service == nil {
		return
	}
	service.deletionObserver = observer
}

func notifyLocalAccountsDeleted(ctx context.Context, observer LocalAccountDeletionObserver, accounts []serviceaccountstore.Account) error {
	if observer == nil || len(accounts) == 0 {
		return nil
	}
	return observer.LocalAccountsDeleted(ctx, accounts)
}
