package deploy

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestGetEntityNameAndVersion(t *testing.T) {
	tests := []struct {
		name        string
		fullName    string
		stackName   string
		expected    string
		expectedVer string
		expectError bool
	}{
		{
			name:        "valid config with version",
			fullName:    "myapp_database_v2",
			stackName:   "myapp",
			expected:    "database",
			expectedVer: "v2",
			expectError: false,
		},
		{
			name:        "valid config with numeric version",
			fullName:    "myapp_redis_1",
			stackName:   "myapp",
			expected:    "redis",
			expectedVer: "1",
			expectError: false,
		},
		{
			name:        "config without underscore in name",
			fullName:    "myapp_db_v1",
			stackName:   "myapp",
			expected:    "db",
			expectedVer: "v1",
			expectError: false,
		},
		{
			name:        "config with multiple underscores",
			fullName:    "myapp_my_database_v3",
			stackName:   "myapp",
			expected:    "my_database",
			expectedVer: "v3",
			expectError: false,
		},
		{
			name:        "invalid config - no version",
			fullName:    "myapp_database",
			stackName:   "myapp",
			expectError: true,
		},
		{
			name:        "empty config name",
			fullName:    "myapp__v1",
			stackName:   "myapp",
			expected:    "",
			expectedVer: "v1",
			expectError: false,
		},
		{
			name:        "wrong stack prefix",
			fullName:    "otherapp_database_v1",
			stackName:   "myapp",
			expected:    "otherapp_database",
			expectedVer: "v1",
			expectError: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			name, version, err := GetEntityNameAndVersion(tt.fullName, tt.stackName)

			if tt.expectError {
				assert.Error(t, err)
				assert.Empty(t, name)
				assert.Empty(t, version)
			} else {
				assert.NoError(t, err)
				assert.Equal(t, tt.expected, name)
				assert.Equal(t, tt.expectedVer, version)
			}
		})
	}
}
