package webrtc

import (
	"testing"

	"github.com/pion/webrtc/v4"
)

func TestBuildICEConfig_Defaults(t *testing.T) {
	c := DefaultConfig()
	rtcConfig, _, err := BuildICEConfig(c)
	if err != nil {
		t.Fatalf("BuildICEConfig: %v", err)
	}
	if len(rtcConfig.ICEServers) == 0 {
		t.Error("ICEServers should not be empty")
	}
	// First default is Cloudflare STUN.
	if rtcConfig.ICEServers[0].URLs[0] != "stun:stun.cloudflare.com:3478" {
		t.Errorf("first ICE server = %q, want stun:stun.cloudflare.com:3478",
			rtcConfig.ICEServers[0].URLs[0])
	}
}

func TestBuildICEConfig_PublicIP(t *testing.T) {
	c := DefaultConfig()
	c.PublicIP = "203.0.113.10"
	_, se, err := BuildICEConfig(c)
	if err != nil {
		t.Fatalf("BuildICEConfig: %v", err)
	}
	if se == nil {
		t.Fatal("SettingEngine should not be nil when PublicIP is set")
	}
	// We can't introspect NAT1To1IPs directly from Pion's public API; the
	// smoke test is that building an API from this engine works.
	api := webrtc.NewAPI(webrtc.WithSettingEngine(*se))
	if api == nil {
		t.Fatal("NewAPI returned nil")
	}
}

func TestBuildICEConfig_InvalidConfig(t *testing.T) {
	c := DefaultConfig()
	c.WHEPListen = ""
	_, _, err := BuildICEConfig(c)
	if err == nil {
		t.Error("BuildICEConfig should reject invalid config")
	}
}
