package testsupport

import (
	"context"
	"net"

	"github.com/testcontainers/testcontainers-go"
)

// PortBindingAvailable reports whether this environment can bind a local TCP listener.
func PortBindingAvailable() bool {
	ln, err := net.Listen("tcp4", "127.0.0.1:0")
	if err != nil {
		return false
	}
	_ = ln.Close()
	return true
}

// DockerAvailable reports whether Testcontainers can talk to a Docker provider.
func DockerAvailable() bool {
	defer func() {
		_ = recover()
	}()

	provider, err := testcontainers.ProviderDocker.GetProvider()
	if err != nil {
		return false
	}
	if err := provider.Health(context.Background()); err != nil {
		return false
	}
	return true
}
