package automation

import (
	"encoding/json"
	"fmt"

	"github.com/nexuscrm/shared/pkg/models"
)

type connectorPushOriginatingAuthorityEnvelope struct {
	models.UserSession
	CredentialID string `json:"credential_id,omitempty"`
}

func decodeConnectorPushOriginatingAuthority(
	raw string,
	snapshot *ConnectorPushDurableSnapshot,
) error {
	if snapshot == nil {
		return fmt.Errorf("connector push originating authority snapshot is nil")
	}
	var envelope connectorPushOriginatingAuthorityEnvelope
	if err := json.Unmarshal([]byte(raw), &envelope); err != nil {
		return fmt.Errorf("decode connector push originating user session: %w", err)
	}
	snapshot.OriginatingUserSession =
		cloneConnectorPushUserSession(envelope.UserSession)
	snapshot.OriginatingCredentialID = envelope.CredentialID
	return nil
}
