package audit

import (
	"testing"
)

var audits = ListAudits{
	Data: []ItemAudit{
		{
			Args:          "[]",
			Call:          "/restmachine/cloudapi/audit/linkedJobs",
			GUID:          "550e8400-e29b-41d4-a716-446655440001",
			CorrelationID: "550e8400-e29b-41d4-a716-446655440001",
			Kwargs:        `{\"audit_guid\":\"dd8623a1-a887-48c1-a500-c10210d404cf\"}`,
			RemoteAddr:    "192.168.1.100",
			ResponseTime:  1,
			Result:        `[]`,
			StatusCode:    200,
			Timestamp:     1640995200,
			TimestampEnd:  1640995201,
			User:          "test@example.com",
			TTL:           "2025-07-31T14:22:57.028000",
		},
		{
			Args:          "[]",
			Call:          "/restmachine/cloudapi/audit/test",
			GUID:          "550e8400-e29b-41d4-a716-446655440002",
			CorrelationID: "550e8400-e29b-41d4-a716-446655440002",
			Kwargs:        `{\"audit_guid\":\"dd8623a1-a887-48c1-a500-c10210d404cf\"}`,
			RemoteAddr:    "192.168.1.105",
			ResponseTime:  5,
			Result:        `[]`,
			StatusCode:    400,
			Timestamp:     1640995200,
			TimestampEnd:  1640995201,
			User:          "test2@example.com",
			TTL:           "2025-07-31T14:22:57.028000",
		},
	},
	EntryCount: 2,
}

func TestFilterByID(t *testing.T) {
	actual := audits.FilterByID("550e8400-e29b-41d4-a716-446655440002").FindOne()

	if actual.GUID != "550e8400-e29b-41d4-a716-446655440002" {
		t.Fatal("expected GUID 550e8400-e29b-41d4-a716-446655440002, found: ", actual.GUID)
	}

	actualEmpty := audits.FilterByID("")

	if len(actualEmpty.Data) != 0 {
		t.Fatal("expected empty, actual: ", len(actualEmpty.Data))
	}
}

func TestFilterByCorrelationID(t *testing.T) {
	actual := audits.FilterByCorrelationID("550e8400-e29b-41d4-a716-446655440002").FindOne()

	if actual.CorrelationID != "550e8400-e29b-41d4-a716-446655440002" {
		t.Fatal("expected GUID 550e8400-e29b-41d4-a716-446655440002, found: ", actual.CorrelationID)
	}

	actualEmpty := audits.FilterByCorrelationID("")

	if len(actualEmpty.Data) != 0 {
		t.Fatal("expected empty, actual: ", len(actualEmpty.Data))
	}
}

func TestFilterByRemoteAddr(t *testing.T) {
	actual := audits.FilterByRemoteAddr("192.168.1.100").FindOne()

	if actual.RemoteAddr != "192.168.1.100" {
		t.Fatal("expected remote address 192.168.1.100, found: ", actual.RemoteAddr)
	}

	actualEmpty := audits.FilterByRemoteAddr("")

	if len(actualEmpty.Data) != 0 {
		t.Fatal("expected empty, actual: ", len(actualEmpty.Data))
	}
}

func TestFilterByUser(t *testing.T) {
	actual := audits.FilterByUser("test@example.com").FindOne()

	if actual.User != "test@example.com" {
		t.Fatal("expected user test@example.com, found: ", actual.RemoteAddr)
	}

	actualEmpty := audits.FilterByUser("")

	if len(actualEmpty.Data) != 0 {
		t.Fatal("expected empty, actual: ", len(actualEmpty.Data))
	}
}

func TestFilterByCall(t *testing.T) {
	actual := audits.FilterByCall("/restmachine/cloudapi/audit/test").FindOne()

	if actual.Call != "/restmachine/cloudapi/audit/test" {
		t.Fatal("expected call /restmachine/cloudapi/audit/test, found: ", actual.Call)
	}
}

func TestFilterByStatusCode(t *testing.T) {
	actual := audits.FilterByStatusCode(200)

	for _, item := range actual.Data {
		if item.StatusCode != 200 {
			t.Fatal("expected 200 status code, found: ", item.StatusCode)
		}
	}
}
