package app

import (
	"os"
	"testing"

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

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

	for _, testCase := range []struct {
		file     string
		contains []string
	}{
		{
			file: "service_event_subscription_lifecycle.go",
			contains: []string{
				"func (store *Store) setServiceEventSubscriptionStatus",
				"func (store *Store) RemoveServiceEventSubscription",
			},
		},
		{
			file: "service_event_subscription_delivery_projection.go",
			contains: []string{
				"func markServiceEventSubscriptionDelivered",
				"func completeServiceEventSubscriptionAfterTerminalEvent",
				"func markServiceEventSubscriptionDeliveryFailure",
			},
		},
		{
			file: "pull_request_subscription_rate_limit.go",
			contains: []string{
				"type pullRequestSubscriptionRateLimitEvent struct",
				"func recordPullRequestSubscriptionRateLimitEvent",
				"func pullRequestSubscriptionRateLimitConfigForAction",
			},
		},
		{
			file: "provider_watch_rate_limit.go",
			contains: []string{
				"func recordProviderWatchRateLimitEvent",
				"return recordPullRequestSubscriptionRateLimitEvent",
				"return pullRequestSubscriptionRateLimitConfigForAction",
			},
		},
		{
			file: "service_event_provider_payload.go",
			contains: []string{
				"func serviceEventDeliveryPayload",
				"func buildServiceEventSourceOccurrenceRef",
				"func providerEventActionSnapshot",
			},
		},
	} {
		t.Run(testCase.file, func(t *testing.T) {
			source := readProviderWatchBoundarySource(t, testCase.file)
			for _, want := range testCase.contains {
				require.Contains(t, source, want)
			}
		})
	}

	serviceSource := readProviderWatchBoundarySource(t, "provider_watch_service.go")
	require.NotContains(t, serviceSource, "func (store *Store) setProviderWatchStatus")
	require.NotContains(t, serviceSource, "func recordProviderWatchRateLimitEvent")

	webhookSource := readProviderWatchBoundarySource(t, "service_event_provider_webhook_handler.go")
	require.NotContains(t, webhookSource, "func reserveProviderWatchTriggerRun")
	require.NotContains(t, webhookSource, "func insertProviderWatchTriggerRun")
	require.NotContains(t, webhookSource, "func markProviderWatchDelivered")
	require.NotContains(t, webhookSource, "func completeProviderWatchAfterTerminalEvent")
	require.NotContains(t, webhookSource, "func markProviderWatchDeliveryFailure")
}

func readProviderWatchBoundarySource(t *testing.T, filename string) string {
	t.Helper()
	content, err := os.ReadFile(filename)
	require.NoError(t, err)
	return string(content)
}
