package acsgroups

import (
	"testing"
)

var testAccessGroups = AccessGroupList{
	AccessGroups: []AccessGroup{
		{
			ID:          "group1",
			DisplayName: "Developers",
			Comment:     "First group",
			CreatedAt:   "2023-01-01",
		},
		{
			ID:          "group2",
			DisplayName: "Admins",
			Comment:     "Second group",
			CreatedAt:   "2023-01-02",
		},
		{
			ID:          "group3",
			DisplayName: "Users",
			Comment:     "Third group",
			CreatedAt:   "2023-01-03",
		},
	},
}

func TestFilterByID(t *testing.T) {
	actual := testAccessGroups.FilterByID("group2").FindOne()

	if actual.ID != "group2" {
		t.Fatal("actual:", actual.ID, "> expected: group2")
	}
}

func TestFilterByName(t *testing.T) {
	actual := testAccessGroups.FilterByName("Users").FindOne()

	if actual.DisplayName != "Users" {
		t.Fatal("actual:", actual.DisplayName, ">> expected: Users")
	}
}

func TestFilterFunc(t *testing.T) {
	actual := testAccessGroups.FilterFunc(func(ag AccessGroup) bool {
		return ag.Comment == "Second group"
	})

	if len(actual.AccessGroups) != 1 || actual.AccessGroups[0].ID != "group2" {
		t.Fatal("Expected 1 group with comment 'Second group', found:", len(actual.AccessGroups))
	}
}

func TestFindOneWithResults(t *testing.T) {
	result := testAccessGroups.FilterByID("group1").FindOne()
	if result.ID != "group1" {
		t.Fatal("Expected group1, got:", result.ID)
	}
}

func TestFindOneEmpty(t *testing.T) {
	emptyList := AccessGroupList{}
	result := emptyList.FindOne()

	if result.ID != "" || result.DisplayName != "" {
		t.Fatal("Expected empty AccessGroup, got:", result)
	}
}

func TestFilterByIDNotFound(t *testing.T) {
	actual := testAccessGroups.FilterByID("nonexistent")

	if len(actual.AccessGroups) != 0 {
		t.Fatal("Expected 0 groups, found:", len(actual.AccessGroups))
	}
}

func TestFilterByNameNotFound(t *testing.T) {
	actual := testAccessGroups.FilterByName("Nonexistent Group")

	if len(actual.AccessGroups) != 0 {
		t.Fatal("Expected 0 groups, found:", len(actual.AccessGroups))
	}
}
