package sep

import (
	"testing"
)

var seps = ListAvailableSEP{
	EntryCount: 3,
	Data: []SEPData{
		{
			SEPID:   1,
			SEPName: "sep_1",
			SEPType: "TATLIN",
			Pools: []Pool{
				{
					Name:   "pool_1",
					Types:  []string{"DES"},
					System: false,
				},
			},
		},
		{
			SEPID:   2,
			SEPName: "sep_2",
			SEPType: "SHARED",
			Pools: []Pool{
				{
					Name:   "pool_2",
					Types:  []string{"DES"},
					System: true,
				},
			},
		},
		{
			SEPID:   3,
			SEPName: "sep_3",
			SEPType: "DES",
			Pools: []Pool{
				{
					Name:   "pool_3",
					Types:  []string{"DES"},
					System: false,
				},
			},
		},
	},
}

func TestFilterBySEPID(t *testing.T) {
	actual := seps.FilterBySEPID(1).FindOne()

	if actual.SEPID != 1 {
		t.Fatal("expected SEPID 1, found: ", actual.SEPID)
	}
}

func TestFilterBySEPName(t *testing.T) {
	actual := seps.FilterBySEPName("sep_2").FindOne()

	if actual.SEPName != "sep_2" {
		t.Fatal("expected SEPName 'sep_2', found: ", actual.SEPName)
	}
}

func TestFilterBySEPType(t *testing.T) {
	actual := seps.FilterBySEPType("TATLIN").FindOne()

	if actual.SEPType != "TATLIN" {
		t.Fatal("expected SEPType 'TATLIN', found: ", actual.SEPType)
	}
}

func TestFilterByPoolType(t *testing.T) {
	actual := seps.FilterByPoolType("DES")

	if len(actual.Data) != 3 {
		t.Fatal("expected 3 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		found := false
		for _, pool := range item.Pools {
			for _, poolType := range pool.Types {
				if poolType == "DES" {
					found = true
					break
				}
			}
			if found {
				break
			}
		}
		if !found {
			t.Fatal("expected Pool type 'DES', not found in SEP: ", item.SEPID)
		}
	}
}

func TestFilterBySystemPool(t *testing.T) {
	actual := seps.FilterBySystemPool(true)

	if len(actual.Data) != 1 {
		t.Fatal("expected 1 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		found := false
		for _, pool := range item.Pools {
			if pool.System {
				found = true
				break
			}
		}
		if !found {
			t.Fatal("expected System pool, not found in SEP: ", item.SEPID)
		}
	}
}

func TestFilterFunc(t *testing.T) {
	actual := seps.FilterFunc(func(sd SEPData) bool {
		return len(sd.Pools) > 0
	})

	if len(actual.Data) != 3 {
		t.Fatal("expected 3 found, actual: ", len(actual.Data))
	}

	for _, item := range actual.Data {
		if len(item.Pools) == 0 {
			t.Fatal("expected Pools to contain at least 1 element, found: ", len(item.Pools))
		}
	}
}
