package container

import (
	"fmt"
	"os"
	"path/filepath"

	"debian-bootc-image-builder/internal/apt"
	"github.com/sirupsen/logrus"
)

// Container represents a container interface for APT operations
type Container interface {
	Root() string
	InitAPT() error
	NewContainerSolver(cacheRoot, arch string, sourceInfo interface{}) (*apt.Solver, error)
	DefaultRootfsType() (string, error)
	Stop() error
}

// PodmanContainer implements the Container interface using Podman
type PodmanContainer struct {
	root string
	// Add other fields as needed
}

// New creates a new PodmanContainer instance
func New(imageRef string) (*PodmanContainer, error) {
	// This is a simplified implementation
	// In a real implementation, this would:
	// 1. Pull the container image if needed
	// 2. Create a container instance
	// 3. Start the container
	// 4. Mount the container filesystem
	
	// For now, we'll simulate this with a temporary directory
	root := filepath.Join("/tmp", "container-root", filepath.Base(imageRef))
	if err := os.MkdirAll(root, 0755); err != nil {
		return nil, fmt.Errorf("cannot create container root: %w", err)
	}

	return &PodmanContainer{
		root: root,
	}, nil
}

// Root returns the container's root filesystem path
func (c *PodmanContainer) Root() string {
	return c.root
}

// InitAPT initializes APT configuration in the container
func (c *PodmanContainer) InitAPT() error {
	logrus.Debugf("Initializing APT in container root: %s", c.root)
	
	// Create necessary APT directories
	aptDirs := []string{
		filepath.Join(c.root, "etc", "apt"),
		filepath.Join(c.root, "etc", "apt", "sources.list.d"),
		filepath.Join(c.root, "var", "lib", "apt"),
		filepath.Join(c.root, "var", "cache", "apt"),
	}
	
	for _, dir := range aptDirs {
		if err := os.MkdirAll(dir, 0755); err != nil {
			return fmt.Errorf("cannot create APT directory %s: %w", dir, err)
		}
	}
	
	// Create basic APT configuration
	aptConf := filepath.Join(c.root, "etc", "apt", "apt.conf")
	conf := `APT::Architecture "amd64";
APT::Get::Assume-Yes "true";
APT::Get::AllowUnauthenticated "true";
`
	
	if err := os.WriteFile(aptConf, []byte(conf), 0644); err != nil {
		return fmt.Errorf("cannot write APT configuration: %w", err)
	}
	
	// Create sources.list for Debian repositories
	sourcesList := filepath.Join(c.root, "etc", "apt", "sources.list")
	sources := `deb http://deb.debian.org/debian trixie main
deb http://security.debian.org/debian-security trixie-security main
deb http://deb.debian.org/debian trixie-updates main
`
	
	if err := os.WriteFile(sourcesList, []byte(sources), 0644); err != nil {
		return fmt.Errorf("cannot write sources.list: %w", err)
	}
	
	return nil
}

// NewContainerSolver creates a new APT solver for this container
func (c *PodmanContainer) NewContainerSolver(cacheRoot, arch string, sourceInfo interface{}) (*apt.Solver, error) {
	// Define default repositories for Debian
	repos := []apt.RepoConfig{
		{
			BaseURL:    "http://deb.debian.org/debian",
			Components: []string{"trixie", "main"},
			Arch:       arch,
			Priority:   500,
		},
		{
			BaseURL:    "http://security.debian.org/debian-security",
			Components: []string{"trixie-security", "main"},
			Arch:       arch,
			Priority:   1000,
		},
		{
			BaseURL:    "http://deb.debian.org/debian",
			Components: []string{"trixie-updates", "main"},
			Arch:       arch,
			Priority:   500,
		},
		{
			BaseURL:    "https://git.raines.xyz/api/packages/particle-os/debian",
			Components: []string{"trixie", "main"},
			Arch:       arch,
			Priority:   400,
		},
	}
	
	return apt.NewSolver(cacheRoot, arch, repos)
}

// DefaultRootfsType returns the default root filesystem type for Debian
func (c *PodmanContainer) DefaultRootfsType() (string, error) {
	// Debian typically uses ext4 as the default filesystem
	return "ext4", nil
}

// Stop stops the container
func (c *PodmanContainer) Stop() error {
	logrus.Debugf("Stopping container with root: %s", c.root)
	// In a real implementation, this would stop the container
	// For now, we'll just log it
	return nil
}
