package br

import (
	"context"
	"testing"

	"github.com/stretchr/testify/mock"
	"github.com/stretchr/testify/require"
	apiv1 "github.com/tidbcloud/cluster-service-ng/api/cluster-service-ng/v1beta"
	"github.com/tidbcloud/cluster-service-ng/domain/entity"
	repomocks "github.com/tidbcloud/cluster-service-ng/pkg/storage/repo/mocks"
	templateversion "github.com/tidbcloud/cluster-service-ng/pkg/template/version"
)

var testTiBRNodePlacement = TiBRNodePlacement{
	NodeSpec:    "4C16G",
	NodeVersion: "1.33",
}

// newTestTemplateVersionSvc creates a *templateversion.Service backed by a mock
// TemplateRegistryRepo that serves template content from the deployments/templates directory.
// This allows tests to call RenderWithVersion without a real database.
func newTestTemplateVersionSvc(t *testing.T) *templateversion.Service {
	t.Helper()
	tplPath := "../../../../deployments/templates"
	templateMap, err := templateversion.LoadTemplatesFromPath(tplPath)
	require.NoError(t, err, "failed to load templates from %s", tplPath)

	mockRepo := repomocks.NewTemplateRegistryRepo(t)
	mockRepo.On("ListByTemplateName", mock.Anything, mock.Anything).Return(
		func(_ context.Context, name string) []*entity.TemplateRegistry {
			content, ok := templateMap[name]
			if !ok {
				return nil
			}
			return []*entity.TemplateRegistry{{
				TemplateVersionInfo: apiv1.TemplateVersionInfo{
					TemplateName: name,
					Version:      "test-sha256",
					Status:       apiv1.TemplateVersionStatus_TEMPLATE_VERSION_STATUS_AVAILABLE,
				},
				Content: content,
			}}
		},
		func(_ context.Context, _ string) error {
			return nil
		},
	)

	return templateversion.NewService(mockRepo, 3)
}
