package structs

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

func TestSortUserAlertsBySeverityAndActiveAt(t *testing.T) {
	now := time.Now()
	alerts := []*UserAlertInfo{
		{AlertName: "Alert1", Message: "Message1", Duration: time.Hour.String(), ActiveAt: now.Add(-2 * time.Hour).Unix(), Value: "Value1", State: AlertStateActive, Severity: AlertSeverityWarning},
		{AlertName: "Alert2", Message: "Message2", Duration: time.Hour.String(), ActiveAt: now.Add(-1 * time.Hour).Unix(), Value: "Value2", State: AlertStateActive, Severity: AlertSeverityMajor},
		{AlertName: "Alert3", Message: "Message3", Duration: time.Hour.String(), ActiveAt: now.Unix(), Value: "Value3", State: AlertStateActive, Severity: AlertSeverityCritical},
		{AlertName: "Alert4", Message: "Message4", Duration: time.Hour.String(), ActiveAt: now.Add(1 * time.Hour).Unix(), Value: "Value4", State: AlertStateActive, Severity: AlertSeverityWarning},
		{AlertName: "Alert5", Message: "Message5", Duration: time.Hour.String(), ActiveAt: now.Add(2 * time.Hour).Unix(), Value: "Value5", State: AlertStateActive, Severity: AlertSeverityMajor},
	}

	expected := []*UserAlertInfo{
		{AlertName: "Alert3", Message: "Message3", Duration: time.Hour.String(), ActiveAt: now.Unix(), Value: "Value3", State: AlertStateActive, Severity: AlertSeverityCritical},
		{AlertName: "Alert5", Message: "Message5", Duration: time.Hour.String(), ActiveAt: now.Add(2 * time.Hour).Unix(), Value: "Value5", State: AlertStateActive, Severity: AlertSeverityMajor},
		{AlertName: "Alert2", Message: "Message2", Duration: time.Hour.String(), ActiveAt: now.Add(-1 * time.Hour).Unix(), Value: "Value2", State: AlertStateActive, Severity: AlertSeverityMajor},
		{AlertName: "Alert4", Message: "Message4", Duration: time.Hour.String(), ActiveAt: now.Add(1 * time.Hour).Unix(), Value: "Value4", State: AlertStateActive, Severity: AlertSeverityWarning},
		{AlertName: "Alert1", Message: "Message1", Duration: time.Hour.String(), ActiveAt: now.Add(-2 * time.Hour).Unix(), Value: "Value1", State: AlertStateActive, Severity: AlertSeverityWarning},
	}

	SortUserAlertsBySeverityAndActiveAt(alerts)

	assert.Equal(t, alerts, expected)
}

func TestSortUserAlertsByActiveAtDesc(t *testing.T) {
	now := time.Now()
	alerts := []*UserAlertInfo{
		{AlertName: "Alert1", Message: "Message1", Duration: time.Hour.String(), ActiveAt: now.Add(-2 * time.Hour).Unix(), Value: "Value1", State: AlertStateActive, Severity: AlertSeverityWarning},
		{AlertName: "Alert2", Message: "Message2", Duration: time.Hour.String(), ActiveAt: now.Add(-1 * time.Hour).Unix(), Value: "Value2", State: AlertStateActive, Severity: AlertSeverityMajor},
		{AlertName: "Alert3", Message: "Message3", Duration: time.Hour.String(), ActiveAt: now.Unix(), Value: "Value3", State: AlertStateActive, Severity: AlertSeverityCritical},
		{AlertName: "Alert4", Message: "Message4", Duration: time.Hour.String(), ActiveAt: now.Add(1 * time.Hour).Unix(), Value: "Value4", State: AlertStateClosed, Severity: AlertSeverityWarning},
		{AlertName: "Alert5", Message: "Message5", Duration: time.Hour.String(), ActiveAt: now.Add(2 * time.Hour).Unix(), Value: "Value5", State: AlertStateClosed, Severity: AlertSeverityMajor},
	}

	expected := []*UserAlertInfo{
		{AlertName: "Alert5", Message: "Message5", Duration: time.Hour.String(), ActiveAt: now.Add(2 * time.Hour).Unix(), Value: "Value5", State: AlertStateClosed, Severity: AlertSeverityMajor},
		{AlertName: "Alert4", Message: "Message4", Duration: time.Hour.String(), ActiveAt: now.Add(1 * time.Hour).Unix(), Value: "Value4", State: AlertStateClosed, Severity: AlertSeverityWarning},
		{AlertName: "Alert3", Message: "Message3", Duration: time.Hour.String(), ActiveAt: now.Unix(), Value: "Value3", State: AlertStateActive, Severity: AlertSeverityCritical},
		{AlertName: "Alert2", Message: "Message2", Duration: time.Hour.String(), ActiveAt: now.Add(-1 * time.Hour).Unix(), Value: "Value2", State: AlertStateActive, Severity: AlertSeverityMajor},
		{AlertName: "Alert1", Message: "Message1", Duration: time.Hour.String(), ActiveAt: now.Add(-2 * time.Hour).Unix(), Value: "Value1", State: AlertStateActive, Severity: AlertSeverityWarning},
	}
	SortUserAlertsByActiveAtDesc(alerts)
	assert.Equal(t, alerts, expected)
}
