package imgroup

import "testing"

func TestBroadcastGroupIDGenerateAndParse(t *testing.T) {
	global, err := GlobalBroadcastGroupID(" LaLu ")
	if err != nil {
		t.Fatalf("GlobalBroadcastGroupID failed: %v", err)
	}
	if global != "hy_lalu_bc_g_v2" {
		t.Fatalf("global group mismatch: %s", global)
	}
	parsedGlobal := Parse(global)
	if parsedGlobal.Kind != KindGlobalBroadcast || parsedGlobal.AppCode != "lalu" || parsedGlobal.RegionID != 0 {
		t.Fatalf("global parse mismatch: %+v", parsedGlobal)
	}

	// v1 旧群在过渡期仍要能解析成同一 Kind：双发与腾讯回调都依赖这一点。
	legacy, err := LegacyGlobalBroadcastGroupIDWithPrefix("", "lalu")
	if err != nil {
		t.Fatalf("LegacyGlobalBroadcastGroupIDWithPrefix failed: %v", err)
	}
	if legacy != "hy_lalu_bc_g" {
		t.Fatalf("legacy global group mismatch: %s", legacy)
	}
	parsedLegacy := Parse(legacy)
	if parsedLegacy.Kind != KindGlobalBroadcast || parsedLegacy.AppCode != "lalu" {
		t.Fatalf("legacy global parse mismatch: %+v", parsedLegacy)
	}

	region, err := RegionBroadcastGroupID("lalu", 1001)
	if err != nil {
		t.Fatalf("RegionBroadcastGroupID failed: %v", err)
	}
	if region != "hy_lalu_bc_r_v2_1001" {
		t.Fatalf("region group mismatch: %s", region)
	}
	parsedRegion := Parse(region)
	if parsedRegion.Kind != KindRegionBroadcast || parsedRegion.AppCode != "lalu" || parsedRegion.RegionID != 1001 {
		t.Fatalf("region parse mismatch: %+v", parsedRegion)
	}

	// v1 旧区域群在过渡期仍要能解析成同一 Kind：双发、切区移除和腾讯回调都依赖这一点。
	legacyRegion, err := LegacyRegionBroadcastGroupIDWithPrefix("", "lalu", 1001)
	if err != nil {
		t.Fatalf("LegacyRegionBroadcastGroupIDWithPrefix failed: %v", err)
	}
	if legacyRegion != "hy_lalu_bc_r_1001" {
		t.Fatalf("legacy region group mismatch: %s", legacyRegion)
	}
	parsedLegacyRegion := Parse(legacyRegion)
	if parsedLegacyRegion.Kind != KindRegionBroadcast || parsedLegacyRegion.AppCode != "lalu" || parsedLegacyRegion.RegionID != 1001 {
		t.Fatalf("legacy region parse mismatch: %+v", parsedLegacyRegion)
	}
}

func TestParseRoomAndRejectInvalidBroadcast(t *testing.T) {
	if parsed := Parse("room-1001"); parsed.Kind != KindRoom || parsed.RoomID != "room-1001" {
		t.Fatalf("room parse mismatch: %+v", parsed)
	}
	if parsed := Parse("hy_lalu_bc_r_0"); parsed.Kind != KindUnknown {
		t.Fatalf("zero region must be rejected: %+v", parsed)
	}
	if parsed := Parse("hy_lalu_bc_r_v2_0"); parsed.Kind != KindUnknown {
		t.Fatalf("zero v2 region must be rejected: %+v", parsed)
	}
	if _, err := RegionBroadcastGroupID("lalu", 0); err == nil {
		t.Fatal("zero region must not generate a region broadcast group")
	}
	if _, err := LegacyRegionBroadcastGroupIDWithPrefix("", "lalu", 0); err == nil {
		t.Fatal("zero region must not generate a legacy region broadcast group")
	}
	if _, err := GlobalBroadcastGroupID("bad app"); err == nil {
		t.Fatal("app_code with spaces must be rejected")
	}
}

func TestRoomGroupIDPreservesRoomID(t *testing.T) {
	roomID := "lalu_2963d975-316e-444d-ad5c-f82fc36c28ca"
	groupID, err := RoomGroupID(roomID)
	if err != nil {
		t.Fatalf("RoomGroupID failed: %v", err)
	}
	if groupID != roomID {
		t.Fatalf("room group mismatch: %s", groupID)
	}
	parsed := Parse(groupID)
	if parsed.Kind != KindRoom || parsed.RoomID != roomID {
		t.Fatalf("room group parse mismatch: %+v", parsed)
	}
}

func TestBroadcastGroupIDWithPrefix(t *testing.T) {
	global, err := GlobalBroadcastGroupIDWithPrefix("test_", "lalu")
	if err != nil {
		t.Fatalf("GlobalBroadcastGroupIDWithPrefix failed: %v", err)
	}
	if global != "test_hy_lalu_bc_g_v2" {
		t.Fatalf("prefixed global group mismatch: %s", global)
	}
	region, err := RegionBroadcastGroupIDWithPrefix("test_", "lalu", 1001)
	if err != nil {
		t.Fatalf("RegionBroadcastGroupIDWithPrefix failed: %v", err)
	}
	if region != "test_hy_lalu_bc_r_v2_1001" {
		t.Fatalf("prefixed region group mismatch: %s", region)
	}
	legacyRegion, err := LegacyRegionBroadcastGroupIDWithPrefix("test_", "lalu", 1001)
	if err != nil {
		t.Fatalf("LegacyRegionBroadcastGroupIDWithPrefix failed: %v", err)
	}
	if legacyRegion != "test_hy_lalu_bc_r_1001" {
		t.Fatalf("prefixed legacy region group mismatch: %s", legacyRegion)
	}
	if parsedLegacy := ParseWithPrefix(legacyRegion, "test_"); parsedLegacy.Kind != KindRegionBroadcast || parsedLegacy.RegionID != 1001 {
		t.Fatalf("prefixed legacy region parse mismatch: %+v", parsedLegacy)
	}

	parsedGlobal := ParseWithPrefix(global, "test_")
	if parsedGlobal.Kind != KindGlobalBroadcast || parsedGlobal.AppCode != "lalu" {
		t.Fatalf("prefixed global parse mismatch: %+v", parsedGlobal)
	}
	parsedRegion := ParseWithPrefix(region, "test_")
	if parsedRegion.Kind != KindRegionBroadcast || parsedRegion.AppCode != "lalu" || parsedRegion.RegionID != 1001 {
		t.Fatalf("prefixed region parse mismatch: %+v", parsedRegion)
	}
	if parsed := ParseWithPrefix("hy_lalu_bc_r_1001", "test_"); parsed.Kind != KindUnknown {
		t.Fatalf("unprefixed broadcast group must be rejected when prefix is configured: %+v", parsed)
	}
	if parsed := ParseWithPrefix("room-1001", "test_"); parsed.Kind != KindRoom || parsed.RoomID != "room-1001" {
		t.Fatalf("room parse with prefix mismatch: %+v", parsed)
	}
	prefixedRoom, err := RoomGroupIDWithPrefix("test_", "lalu_2963d975-316e-444d-ad5c-f82fc36c28ca")
	if err != nil {
		t.Fatalf("RoomGroupIDWithPrefix failed: %v", err)
	}
	if prefixedRoom != "test_lalu_2963d975-316e-444d-ad5c-f82fc36c28ca" {
		t.Fatalf("prefixed room group mismatch: %s", prefixedRoom)
	}
	if parsed := ParseWithPrefix(prefixedRoom, "test_"); parsed.Kind != KindRoom || parsed.RoomID != "lalu_2963d975-316e-444d-ad5c-f82fc36c28ca" {
		t.Fatalf("prefixed room parse mismatch: %+v", parsed)
	}
}
