package unread

import "testing"

func TestApplyMessageAllowsAutomationOwnerUnreadWhenInAudience(t *testing.T) {
	runtime := newTestRuntime(t, Config{})
	if err := runtime.WarmScopeRoster("w1", "s1", ScopeTypeChannel, []RosterMember{
		{MemberID: "owner", Visibility: "public", NotificationState: "active"},
		{MemberID: "recipient", Visibility: "public", NotificationState: "active"},
	}); err != nil {
		t.Fatalf("WarmScopeRoster returned error: %v", err)
	}
	result, err := runtime.ApplyMessage(MessageStored{
		WorkspaceID: "w1", ScopeID: "s1", ScopeType: ScopeTypeChannel, Seq: 1,
		AuthorMemberID: "owner", NotifyPrincipalMemberID: "owner", AllowAuthorUnread: true,
	})
	if err != nil {
		t.Fatalf("ApplyMessage returned error: %v", err)
	}
	if result.AccountedMembers != 2 {
		t.Fatalf("AccountedMembers = %d, want owner and recipient", result.AccountedMembers)
	}
	for _, memberID := range []string{"owner", "recipient"} {
		if got := runtime.ScopeUnreadCounts("w1", memberID, nil)["s1"]; got != 1 {
			t.Fatalf("%s unread count = %d, want 1", memberID, got)
		}
	}
}

func TestApplyMessageRewoundRemovesAutomationOwnerUnread(t *testing.T) {
	runtime := newTestRuntime(t, Config{})
	if err := runtime.WarmScopeRoster("w1", "s1", ScopeTypeChannel, []RosterMember{
		{MemberID: "owner", Visibility: "public", NotificationState: "active"},
		{MemberID: "recipient", Visibility: "public", NotificationState: "active"},
	}); err != nil {
		t.Fatalf("WarmScopeRoster returned error: %v", err)
	}
	if _, err := runtime.ApplyMessage(MessageStored{
		WorkspaceID: "w1", ScopeID: "s1", ScopeType: ScopeTypeChannel, Seq: 1,
		AuthorMemberID: "owner", NotifyPrincipalMemberID: "owner", AllowAuthorUnread: true,
	}); err != nil {
		t.Fatalf("ApplyMessage returned error: %v", err)
	}
	result, err := runtime.ApplyMessageRewound(MessageRewound{
		WorkspaceID: "w1", RewindGeneration: 1,
		Scopes: []RewoundScope{{ScopeID: "s1", LatestSeq: 0, RemovedMessages: []RewoundMessage{{
			ScopeID: "s1", Seq: 1, AuthorMemberID: "owner", NotifyPrincipalMemberID: "owner",
			AllowAuthorUnread: true, MessageType: "text",
		}}}},
	})
	if err != nil {
		t.Fatalf("ApplyMessageRewound returned error: %v", err)
	}
	if result.Pending || result.Ignored {
		t.Fatalf("ApplyMessageRewound result = %+v, want applied", result)
	}
	for _, memberID := range []string{"owner", "recipient"} {
		if got := runtime.ScopeUnreadCounts("w1", memberID, nil)["s1"]; got != 0 {
			t.Fatalf("%s unread after rewind = %d, want 0", memberID, got)
		}
	}
}

func TestTaskAutomationOwnerUnreadStillUsesFrozenAudience(t *testing.T) {
	runtime := newTestRuntime(t, Config{})
	if err := runtime.WarmScopeRoster("w1", "task-scope", ScopeTypeTask, []RosterMember{
		{MemberID: "owner", Visibility: "public", NotificationState: "active"},
		{MemberID: "recipient", Visibility: "public", NotificationState: "active"},
		{MemberID: "other", Visibility: "public", NotificationState: "active"},
	}); err != nil {
		t.Fatalf("WarmScopeRoster returned error: %v", err)
	}
	result, err := runtime.ApplyMessage(MessageStored{
		WorkspaceID: "w1", ScopeID: "task-scope", ScopeType: ScopeTypeTask, Seq: 1,
		AuthorMemberID: "owner", NotifyPrincipalMemberID: "owner", AllowAuthorUnread: true,
		RecipientMemberIDs: []string{"owner", "recipient"},
	})
	if err != nil {
		t.Fatalf("ApplyMessage returned error: %v", err)
	}
	if result.AccountedMembers != 2 {
		t.Fatalf("AccountedMembers = %d, want frozen owner and recipient", result.AccountedMembers)
	}
	for _, memberID := range []string{"owner", "recipient"} {
		if got := runtime.ScopeUnreadCounts("w1", memberID, nil)["task-scope"]; got != 1 {
			t.Fatalf("%s unread count = %d, want 1", memberID, got)
		}
	}
	if got := runtime.ScopeUnreadCounts("w1", "other", nil)["task-scope"]; got != 0 {
		t.Fatalf("non-recipient Task unread count = %d, want 0", got)
	}
}
