package deployment

import (
	"testing"

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

func TestNewSelfDeployer(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	assert.NotNil(t, deployer)
	assert.Equal(t, "1.0.0", deployer.GetCurrentVersion())
}

func TestBuildContainer(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deploymentID, err := deployer.BuildContainer("2.0.0")
	assert.NoError(t, err)
	assert.NotEmpty(t, deploymentID)
	
	deployment, exists := deployer.GetDeploymentInfo(deploymentID)
	assert.True(t, exists)
	assert.Equal(t, "2.0.0", deployment.Version)
}

func TestPushImage(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deploymentID, _ := deployer.BuildContainer("2.0.0")
	err := deployer.PushImage(deploymentID)
	
	assert.NoError(t, err)
}

func TestGenerateManifest(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deploymentID, _ := deployer.BuildContainer("2.0.0")
	manifest, err := deployer.GenerateManifest(deploymentID, 3)
	
	assert.NoError(t, err)
	assert.NotEmpty(t, manifest)
	assert.Contains(t, manifest, "replicas: 3")
}

func TestDeploy(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deploymentID, _ := deployer.BuildContainer("2.0.0")
	deployer.PushImage(deploymentID)
	deployer.GenerateManifest(deploymentID, 3)
	err := deployer.Deploy(deploymentID)
	
	assert.NoError(t, err)
	assert.Equal(t, "2.0.0", deployer.GetCurrentVersion())
}

func TestRollback(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deployer.SetVersion("2.0.0")
	err := deployer.Rollback("1.0.0")
	
	assert.NoError(t, err)
	assert.Equal(t, "1.0.0", deployer.GetCurrentVersion())
}

func TestHealthCheck(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deploymentID, _ := deployer.BuildContainer("2.0.0")
	deployer.PushImage(deploymentID)
	deployer.GenerateManifest(deploymentID, 3)
	deployer.Deploy(deploymentID)
	
	healthy, err := deployer.HealthCheck(deploymentID)
	assert.NoError(t, err)
	assert.True(t, healthy)
}

func TestListDeployments(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deployer.BuildContainer("2.0.0")
	deployer.BuildContainer("2.0.1")
	
	deployments := deployer.ListDeployments()
	assert.Equal(t, 2, len(deployments))
}

func TestBuildContainerError(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	_, err := deployer.BuildContainer("")
	assert.Error(t, err)
}

func TestPushImageError(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	err := deployer.PushImage("nonexistent")
	assert.Error(t, err)
}

func TestFullDeploymentCycle(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	// Build
	deploymentID, _ := deployer.BuildContainer("2.0.0")
	
	// Push
	deployer.PushImage(deploymentID)
	
	// Generate manifest
	deployer.GenerateManifest(deploymentID, 3)
	
	// Deploy
	err := deployer.Deploy(deploymentID)
	assert.NoError(t, err)
	
	// Verify
	assert.Equal(t, "2.0.0", deployer.GetCurrentVersion())
	
	// Health check
	healthy, _ := deployer.HealthCheck(deploymentID)
	assert.True(t, healthy)
}

func TestSetVersion(t *testing.T) {
	deployer := NewSelfDeployer("docker.io", "/etc/kubernetes/config")
	
	deployer.SetVersion("3.0.0")
	assert.Equal(t, "3.0.0", deployer.GetCurrentVersion())
}
