package testenv

import (
	"context"
	"testing"

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

// StartNetwork creates a user-defined Docker bridge network and
// registers a t.Cleanup to remove it. All platform containers attach
// to the same network so they can resolve each other by alias.
//
// A failure here is fatal, not a skip: the network create path runs
// long after `RequireDocker` has confirmed the daemon is reachable, so
// any error here is a real environment break (subnet exhaustion, a
// half-dead Ryuk reaper, a daemon-side network plugin issue) and
// silently skipping it would mask the rest of the suite as
// "passing" when nothing in fact ran.
func StartNetwork(t *testing.T) *testcontainers.DockerNetwork {
	t.Helper()
	ctx := context.Background()
	net, err := tcnetwork.New(ctx)
	if err != nil {
		t.Fatalf("create docker network: %v", err)
	}
	t.Cleanup(func() {
		if err := net.Remove(ctx); err != nil {
			t.Logf("remove network: %v", err)
		}
	})
	return net
}
