// ABOUTME: Integration test with the actual EdgeConnectConfig.yaml example file
// ABOUTME: Validates that our parser correctly handles the real example configuration
package config

import (
	"path/filepath"
	"testing"

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

func TestParseExampleConfig(t *testing.T) {
	parser := NewParser()

	// Parse the actual example file (now that we've created the manifest file)
	examplePath := filepath.Join("../../sdk/examples/comprehensive/EdgeConnectConfig_v1.yaml")
	config, parsedManifest, err := parser.ParseFile(examplePath)

	// This should now succeed with full validation
	require.NoError(t, err)
	require.NotNil(t, config)
	require.NotEmpty(t, parsedManifest)

	// Validate the parsed structure
	assert.Equal(t, "edgeconnect-deployment", config.Kind)
	assert.Equal(t, "edge-app-demo", config.Metadata.Name)

	// Check k8s app configuration
	require.NotNil(t, config.Spec.K8sApp)
	assert.Equal(t, "1.0.0", config.Metadata.AppVersion)
	// Note: ManifestFile path should be resolved to absolute path
	assert.Contains(t, config.Spec.K8sApp.ManifestFile, "k8s-deployment.yaml")

	// Check infrastructure template
	require.Len(t, config.Spec.InfraTemplate, 1)
	infra := config.Spec.InfraTemplate[0]
	assert.Equal(t, "EU", infra.Region)
	assert.Equal(t, "TelekomOP", infra.CloudletOrg)
	assert.Equal(t, "Munich", infra.CloudletName)
	assert.Equal(t, "EU.small", infra.FlavorName)

	// Check network configuration
	require.NotNil(t, config.Spec.Network)
	require.Len(t, config.Spec.Network.OutboundConnections, 2)

	conn1 := config.Spec.Network.OutboundConnections[0]
	assert.Equal(t, "tcp", conn1.Protocol)
	assert.Equal(t, 80, conn1.PortRangeMin)
	assert.Equal(t, 80, conn1.PortRangeMax)
	assert.Equal(t, "0.0.0.0/0", conn1.RemoteCIDR)

	conn2 := config.Spec.Network.OutboundConnections[1]
	assert.Equal(t, "tcp", conn2.Protocol)
	assert.Equal(t, 443, conn2.PortRangeMin)
	assert.Equal(t, 443, conn2.PortRangeMax)
	assert.Equal(t, "0.0.0.0/0", conn2.RemoteCIDR)

	// Test utility methods
	assert.Equal(t, "edge-app-demo", config.Metadata.Name)
	assert.Contains(t, config.Spec.GetManifestFile(), "k8s-deployment.yaml")
	assert.True(t, config.Spec.IsK8sApp())
	assert.False(t, config.Spec.IsDockerApp())
}

func TestValidateExampleStructure(t *testing.T) {
	parser := &ConfigParser{}

	// Create a config that matches the example but with valid paths
	config := &EdgeConnectConfig{
		Kind: "edgeconnect-deployment",
		Metadata: Metadata{
			Name:         "edge-app-demo",
			AppVersion:   "1.0.0",
			Organization: "edp2",
		},
		Spec: Spec{
			DockerApp: &DockerApp{ // Use DockerApp to avoid manifest file validation
				Image: "nginx:latest",
			},
			InfraTemplate: []InfraTemplate{
				{
					Region:       "EU",
					CloudletOrg:  "TelekomOP",
					CloudletName: "Munich",
					FlavorName:   "EU.small",
				},
			},
			Network: &NetworkConfig{
				OutboundConnections: []OutboundConnection{
					{
						Protocol:     "tcp",
						PortRangeMin: 80,
						PortRangeMax: 80,
						RemoteCIDR:   "0.0.0.0/0",
					},
					{
						Protocol:     "tcp",
						PortRangeMin: 443,
						PortRangeMax: 443,
						RemoteCIDR:   "0.0.0.0/0",
					},
				},
			},
		},
	}

	// This should validate successfully
	err := parser.Validate(config)
	assert.NoError(t, err)
}
