package pods

import (
	"log"
	"time"

	"github.com/containers/podman/v4/pkg/bindings/volumes"
	"github.com/spf13/viper"
)

// GarbageCollector is a goroutine that cleans up old Containers
func GarbageCollector() error {
	if viper.GetBool("timeout_on_restart") {
		timeoutExistingContainers()
	}
	for {
		err := Cleanup()
		if err != nil {
			return err
		}
		time.Sleep(time.Minute * 10)
	}
}

func timeoutExistingContainers() {
	var oldContainers []string

	for _, container := range containerList() {
		oldContainers = append(oldContainers, container.ID)
	}
	OldContainers = append(OldContainers, oldContainers...)
	log.Println("old Containers: ", oldContainers)

}

func pruneVolumes() error {

	results, err := volumes.Prune(Socket, nil)
	if err != nil {
		return err
	}
	resultLen := len(results)
	for i, result := range results {
		log.Printf("[%d/%d] %s %d MB", i, resultLen, result.Id, result.Size/1024/1024)
	}
	return nil

}
