package container

import (
	"testing"

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

func TestPodmanContainer(t *testing.T) {
	// Test creating a new PodmanContainer
	container := NewPodmanContainer()
	assert.NotNil(t, container)
}

func TestPodmanContainerInitAPT(t *testing.T) {
	container := NewPodmanContainer()
	
	// Test InitAPT (should not error)
	err := container.InitAPT()
	assert.NoError(t, err)
}

func TestPodmanContainerNewContainerSolver(t *testing.T) {
	container := NewPodmanContainer()
	
	// Test creating a new container solver
	solver := container.NewContainerSolver()
	assert.NotNil(t, solver)
	
	// Test that the solver has repositories configured
	assert.NotNil(t, solver.repos)
	assert.Len(t, solver.repos, 2)
	
	// Check that debian repository is configured
	var debianRepo, forgeRepo bool
	for _, repo := range solver.repos {
		if repo.Name == "debian" {
			debianRepo = true
			assert.Equal(t, "http://deb.debian.org/debian", repo.BaseURL)
			assert.True(t, repo.Enabled)
			assert.Equal(t, 500, repo.Priority)
		}
		if repo.Name == "debian-forge" {
			forgeRepo = true
			assert.Equal(t, "https://git.raines.xyz/api/packages/particle-os/debian", repo.BaseURL)
			assert.True(t, repo.Enabled)
			assert.Equal(t, 400, repo.Priority)
		}
	}
	
	assert.True(t, debianRepo, "debian repository should be configured")
	assert.True(t, forgeRepo, "debian-forge repository should be configured")
}

func TestPodmanContainerDefaultRootfsType(t *testing.T) {
	container := NewPodmanContainer()
	
	// Test getting default rootfs type
	rootfsType := container.DefaultRootfsType()
	assert.Equal(t, "ext4", rootfsType)
}

func TestContainerInterface(t *testing.T) {
	// Test that PodmanContainer implements the Container interface
	var _ Container = NewPodmanContainer()
}

func TestPodmanContainerSolverIntegration(t *testing.T) {
	container := NewPodmanContainer()
	solver := container.NewContainerSolver()
	
	// Test that the solver can be used for depsolving
	packages := []string{"base-files", "systemd", "linux-image-amd64"}
	result, err := solver.Depsolve(packages, 0)
	assert.NoError(t, err)
	assert.NotNil(t, result)
	assert.Equal(t, packages, result.Packages)
}

func TestPodmanContainerRepositoryConfiguration(t *testing.T) {
	container := NewPodmanContainer()
	solver := container.NewContainerSolver()
	
	// Test repository configuration
	repos := solver.repos
	assert.Len(t, repos, 2)
	
	// Test debian repository
	debianRepo := repos[0]
	assert.Equal(t, "debian", debianRepo.Name)
	assert.Equal(t, "http://deb.debian.org/debian", debianRepo.BaseURL)
	assert.True(t, debianRepo.Enabled)
	assert.Equal(t, 500, debianRepo.Priority)
	
	// Test debian-forge repository
	forgeRepo := repos[1]
	assert.Equal(t, "debian-forge", forgeRepo.Name)
	assert.Equal(t, "https://git.raines.xyz/api/packages/particle-os/debian", forgeRepo.BaseURL)
	assert.True(t, forgeRepo.Enabled)
	assert.Equal(t, 400, forgeRepo.Priority)
}

func TestPodmanContainerMultipleInstances(t *testing.T) {
	// Test creating multiple container instances
	container1 := NewPodmanContainer()
	container2 := NewPodmanContainer()
	
	assert.NotNil(t, container1)
	assert.NotNil(t, container2)
	
	// Test that they are independent
	solver1 := container1.NewContainerSolver()
	solver2 := container2.NewContainerSolver()
	
	assert.NotNil(t, solver1)
	assert.NotNil(t, solver2)
	
	// Test that both solvers work independently
	packages := []string{"base-files", "systemd"}
	result1, err1 := solver1.Depsolve(packages, 0)
	result2, err2 := solver2.Depsolve(packages, 0)
	
	assert.NoError(t, err1)
	assert.NoError(t, err2)
	assert.Equal(t, result1.Packages, result2.Packages)
}

func TestPodmanContainerErrorHandling(t *testing.T) {
	container := NewPodmanContainer()
	
	// Test that InitAPT doesn't fail
	err := container.InitAPT()
	assert.NoError(t, err)
	
	// Test that NewContainerSolver always returns a valid solver
	solver := container.NewContainerSolver()
	assert.NotNil(t, solver)
	
	// Test that DefaultRootfsType always returns a valid type
	rootfsType := container.DefaultRootfsType()
	assert.NotEmpty(t, rootfsType)
	assert.Equal(t, "ext4", rootfsType)
}

func TestPodmanContainerSolverWithEmptyPackages(t *testing.T) {
	container := NewPodmanContainer()
	solver := container.NewContainerSolver()
	
	// Test depsolving with empty package list
	result, err := solver.Depsolve([]string{}, 0)
	assert.NoError(t, err)
	assert.NotNil(t, result)
	assert.Empty(t, result.Packages)
}

func TestPodmanContainerSolverWithLargePackageList(t *testing.T) {
	container := NewPodmanContainer()
	solver := container.NewContainerSolver()
	
	// Test depsolving with a large package list
	packages := make([]string, 1000)
	for i := 0; i < 1000; i++ {
		packages[i] = "package-" + string(rune('a'+i%26))
	}
	
	result, err := solver.Depsolve(packages, 0)
	assert.NoError(t, err)
	assert.NotNil(t, result)
	assert.Equal(t, packages, result.Packages)
}
