package extensionflow

import (
	"errors"
	"fmt"
)

const (
	CatalogReconcileErrorUnregisteredDigest = "unregistered_digest"
	CatalogReconcileErrorCorruptCatalog     = "corrupt_catalog"
)

// CatalogReconcileBlockedError marks deterministic configuration/state errors
// that must not be retried per Agent. The Workspace reconcile job records one
// blocked diagnostic; a later source mutation can enqueue a newer generation.
type CatalogReconcileBlockedError struct {
	Code    string
	Message string
}

func (e *CatalogReconcileBlockedError) Error() string {
	if e == nil {
		return ""
	}
	if e.Message == "" {
		return e.Code
	}
	return fmt.Sprintf("%s: %s", e.Code, e.Message)
}

func newCatalogReconcileBlockedError(code string, format string, args ...any) error {
	return &CatalogReconcileBlockedError{
		Code:    code,
		Message: fmt.Sprintf(format, args...),
	}
}

func catalogReconcileBlocked(err error) (string, string, bool) {
	var blocked *CatalogReconcileBlockedError
	if !errors.As(err, &blocked) || blocked == nil {
		return "", "", false
	}
	return blocked.Code, blocked.Message, true
}
