package roomadmin

import (
	"errors"
	"reflect"
	"testing"
)

func TestNormalizeRoomConfigSortsAndKeepsFixedCandidates(t *testing.T) {
	config, err := normalizeRoomConfig(RoomConfig{
		AllowedSeatCounts: []int32{30, 10, 20},
		DefaultSeatCount:  20,
		UpdatedAtMS:       123,
	})
	if err != nil {
		t.Fatalf("normalizeRoomConfig failed: %v", err)
	}
	if !reflect.DeepEqual(config.CandidateSeatCounts, []int32{10, 15, 20, 25, 30}) {
		t.Fatalf("candidate seat counts mismatch: %+v", config.CandidateSeatCounts)
	}
	if !reflect.DeepEqual(config.AllowedSeatCounts, []int32{10, 20, 30}) {
		t.Fatalf("allowed seat counts should be sorted: %+v", config.AllowedSeatCounts)
	}
	if config.DefaultSeatCount != 20 || config.UpdatedAtMS != 123 {
		t.Fatalf("config fields changed unexpectedly: %+v", config)
	}
}

func TestNormalizeRoomConfigRejectsInvalidValues(t *testing.T) {
	tests := []struct {
		name   string
		config RoomConfig
	}{
		{name: "empty", config: RoomConfig{AllowedSeatCounts: nil, DefaultSeatCount: 15}},
		{name: "duplicate", config: RoomConfig{AllowedSeatCounts: []int32{10, 10}, DefaultSeatCount: 10}},
		{name: "outside_candidate", config: RoomConfig{AllowedSeatCounts: []int32{12}, DefaultSeatCount: 12}},
		{name: "default_not_allowed", config: RoomConfig{AllowedSeatCounts: []int32{10, 20}, DefaultSeatCount: 15}},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			_, err := normalizeRoomConfig(test.config)
			if !errors.Is(err, ErrInvalidArgument) {
				t.Fatalf("normalizeRoomConfig should reject %s with ErrInvalidArgument, got %v", test.name, err)
			}
		})
	}
}
