package metadata

import (
	"context"
	"errors"
	"testing"

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

// failingActionsRepo is a MetadataRepository stub whose action-list read
// always fails, simulating a _System_Action store outage.
type failingActionsRepo struct {
	ports.MetadataRepository
}

func (r *failingActionsRepo) GetAllActions(_ context.Context) ([]*models.ActionMetadata, error) {
	return nil, errors.New("store unavailable")
}

// TestGetAllActionsAndError_PropagatesReadFailure pins the two action-list
// contracts side by side: GetAllActionsAndError propagates a repository read
// failure (execution-critical callers must distinguish "no match" from
// "metadata store unavailable" — a false not-found would consume a flow's
// fault connector), while the legacy GetAllActions keeps its log-and-empty
// shape for callers where the distinction does not change behavior.
func TestGetAllActionsAndError_PropagatesReadFailure(t *testing.T) {
	ms := &MetadataService{repo: &failingActionsRepo{}}

	if _, err := ms.GetAllActionsAndError(context.Background()); err == nil {
		t.Fatal("GetAllActionsAndError must propagate the repo read failure, got nil")
	}

	if got := ms.GetAllActions(context.Background()); len(got) != 0 {
		t.Fatalf("GetAllActions = %d items, want empty slice under the legacy swallowing contract", len(got))
	}
}
