package debianpatch

import (
	"fmt"
	"github.com/osbuild/images/pkg/dnfjson"
	"github.com/osbuild/images/pkg/rpmmd"
)

// ConvertDebianPackageSetToRPM converts our DebianPackageSet to rpmmd.PackageSet
// This is needed for compatibility with the osbuild images package
func ConvertDebianPackageSetToRPM(debianSet DebianPackageSet) rpmmd.PackageSet {
	return rpmmd.PackageSet{
		Include: debianSet.Include,
		Exclude: debianSet.Exclude,
	}
}

// ConvertDebianDepsolveResultToDNF converts our DebianDepsolveResult to dnfjson.DepsolveResult
// This is needed for compatibility with the osbuild images package
func ConvertDebianDepsolveResultToDNF(debianResult DebianDepsolveResult) dnfjson.DepsolveResult {
	// Convert DebianRepoConfig to rpmmd.RepoConfig
	repos := make([]rpmmd.RepoConfig, len(debianResult.Repos))
	for i, repo := range debianResult.Repos {
		enabled := repo.Enabled
		priority := repo.Priority
		gpgCheck := repo.GPGCheck
		repos[i] = rpmmd.RepoConfig{
			Name:         repo.Name,
			BaseURLs:     repo.BaseURLs,
			Enabled:      &enabled,
			CheckGPG:     &gpgCheck,
			Priority:     &priority,
			SSLCACert:    repo.SSLCACert,
			SSLClientKey: repo.SSLClientKey,
			SSLClientCert: repo.SSLClientCert,
		}
	}

	// Convert Debian package names to RPM PackageSpec format
	// For now, we'll create basic PackageSpec objects
	packages := make([]rpmmd.PackageSpec, len(debianResult.Packages))
	for i, pkgName := range debianResult.Packages {
		packages[i] = rpmmd.PackageSpec{
			Name:    pkgName,
			Epoch:   0,    // Debian doesn't use epochs like RPM
			Version: "",   // Will be resolved during build
			Release: "",   // Will be resolved during build
			Arch:    "",   // Will be resolved during build
		}
	}

	return dnfjson.DepsolveResult{
		Packages: packages,
		Repos:    repos,
	}
}

// ConvertDebianRepoConfigToRPM converts our DebianRepoConfig to rpmmd.RepoConfig
func ConvertDebianRepoConfigToRPM(debianRepo DebianRepoConfig) rpmmd.RepoConfig {
	enabled := debianRepo.Enabled
	priority := debianRepo.Priority
	gpgCheck := debianRepo.GPGCheck
	return rpmmd.RepoConfig{
		Name:         debianRepo.Name,
		BaseURLs:     debianRepo.BaseURLs,
		Enabled:      &enabled,
		CheckGPG:     &gpgCheck,
		Priority:     &priority,
		SSLCACert:    debianRepo.SSLCACert,
		SSLClientKey: debianRepo.SSLClientKey,
		SSLClientCert: debianRepo.SSLClientCert,
	}
}

// ConvertDNFDepsolveResultToDebian converts dnfjson.DepsolveResult to our DebianDepsolveResult
// This is needed for compatibility when reading from the osbuild images package
func ConvertDNFDepsolveResultToDebian(dnfResult dnfjson.DepsolveResult) DebianDepsolveResult {
	// Convert rpmmd.RepoConfig to DebianRepoConfig
	repos := make([]DebianRepoConfig, len(dnfResult.Repos))
	for i, repo := range dnfResult.Repos {
		enabled := false
		if repo.Enabled != nil {
			enabled = *repo.Enabled
		}
		priority := 0
		if repo.Priority != nil {
			priority = *repo.Priority
		}
		gpgCheck := false
		if repo.CheckGPG != nil {
			gpgCheck = *repo.CheckGPG
		}
		repos[i] = DebianRepoConfig{
			Name:          repo.Name,
			BaseURLs:      repo.BaseURLs,
			Enabled:       enabled,
			GPGCheck:      gpgCheck,
			Priority:      priority,
			SSLClientKey:  repo.SSLClientKey,
			SSLClientCert: repo.SSLClientCert,
			SSLCACert:     repo.SSLCACert,
		}
	}

	// Convert RPM PackageSpec to Debian package names
	packages := make([]string, len(dnfResult.Packages))
	for i, pkg := range dnfResult.Packages {
		packages[i] = pkg.Name
	}

	return DebianDepsolveResult{
		Packages: packages,
		Repos:    repos,
	}
}

// CreateDebianPackageSet creates a DebianPackageSet from package names
func CreateDebianPackageSet(packages []string) DebianPackageSet {
	return DebianPackageSet{
		Include: packages,
		Exclude: []string{},
	}
}

// CreateDebianDepsolveResult creates a DebianDepsolveResult from packages and repos
func CreateDebianDepsolveResult(packages []string, repos []DebianRepoConfig) DebianDepsolveResult {
	return DebianDepsolveResult{
		Packages: packages,
		Repos:    repos,
	}
}

// ValidateDebianPackageSet validates a DebianPackageSet
func ValidateDebianPackageSet(pkgSet DebianPackageSet) error {
	if len(pkgSet.Include) == 0 {
		return fmt.Errorf("package set must include at least one package")
	}
	
	// Check for duplicate packages
	seen := make(map[string]bool)
	for _, pkg := range pkgSet.Include {
		if seen[pkg] {
			return fmt.Errorf("duplicate package in include list: %s", pkg)
		}
		seen[pkg] = true
	}
	
	return nil
}

// ValidateDebianRepoConfig validates a DebianRepoConfig
func ValidateDebianRepoConfig(repo DebianRepoConfig) error {
	if repo.Name == "" {
		return fmt.Errorf("repository name cannot be empty")
	}
	
	if len(repo.BaseURLs) == 0 {
		return fmt.Errorf("repository must have base URLs")
	}
	
	for _, url := range repo.BaseURLs {
		if url == "" {
			return fmt.Errorf("repository base URL cannot be empty")
		}
	}
	
	return nil
}
