package fanout

import (
	"testing"

	"github.com/stretchr/testify/require"

	"github.com/tidbcloud/AgentTeam/internal/store/message"
	"github.com/tidbcloud/AgentTeam/internal/unread"
)

func TestRecoveryDeliveryKeepsWorkflowAutomationIdentity(t *testing.T) {
	source := message.ContributionSourceForWorkflowRun(message.WorkflowRunContributionSourceParams{
		RunID: "wri_recovery_identity", EffectID: "wfx_recovery_identity", PlanID: "pln_recovery_identity", WorkflowName: "Recovery workflow",
	})
	projected := receiveMessageForFanoutDelivery(message.MessageDelivery{
		Message: message.ReceiveMessage{
			Source: source, AuthorProfile: message.AuthorProfile{ActorType: message.AuthorActorTypeHuman, MemberID: "mem_owner", DisplayName: "Owner"},
		},
	}, message.AgentRecipient{})
	require.Equal(t, source, projected.Source)
	require.Equal(t, message.AuthorServiceKindWorkflow, projected.AuthorProfile.ServiceKind)
	require.NotEqual(t, "mem_owner", projected.AuthorProfile.MemberID)
}

func TestDeliveryFanoutAllowsAutomationOwnerUnreadForWorkflowAndScheduleSources(t *testing.T) {
	t.Parallel()

	sources := []message.ContributionSource{
		message.ContributionSourceForWorkflowRun(message.WorkflowRunContributionSourceParams{
			RunID: "wri_unread", EffectID: "wfx_unread", PlanID: "pln_unread",
		}),
		message.ContributionSourceForAutomationRun(message.AutomationRunContributionSourceParams{
			RunID: "arun_unread", AutomationID: "sch_unread",
		}),
	}
	for _, source := range sources {
		source := source
		t.Run(string(source.Kind), func(t *testing.T) {
			runtime, err := unread.New(unread.Config{})
			require.NoError(t, err)
			require.NoError(t, runtime.WarmScopeRoster("wsp_automation_unread", "chn_automation_unread", unread.ScopeTypeChannel, []unread.RosterMember{
				{MemberID: "mem_owner", Visibility: "public", NotificationState: "active"},
				{MemberID: "mem_recipient", Visibility: "public", NotificationState: "active"},
			}))
			unread := &capturingUnreadRuntime{runtime: runtime}
			fanout := NewDeliveryFanout(nil, nil, nil).WithUnreadRuntime(unread)
			fanout.EmitCommittedMessage(t.Context(), "wsp_automation_unread", message.AppMessage{
				ID: "msg_automation_unread", Seq: 12, ChannelID: "chn_automation_unread",
				ChannelType: "channel", Source: source,
				AuthorProfile: message.AuthorProfile{
					ActorType:   message.AuthorActorTypeService,
					ServiceKind: message.AuthorServiceKindWorkflow,
				},
			}, message.MessageDelivery{
				MessageID: "msg_automation_unread", Seq: 12,
				NotifyPrincipalMemberID: "mem_owner",
				Message: message.ReceiveMessage{
					MessageID: "msg_automation_unread", TargetKey: "chn_automation_unread",
					ChannelType: "channel", NotifyPrincipalMemberID: "mem_owner", Source: source,
				},
			})
			require.Len(t, unread.messages, 1)
			require.True(t, unread.messages[0].AllowAuthorUnread)
			require.Equal(t, int64(1), runtime.ScopeUnreadCounts("wsp_automation_unread", "mem_owner", nil)["chn_automation_unread"])
			require.Equal(t, int64(1), runtime.ScopeUnreadCounts("wsp_automation_unread", "mem_recipient", nil)["chn_automation_unread"])
		})
	}
}
