package store_test

import (
	"context"
	"testing"
	"time"

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

func TestChannelCaptionMemory_SuppressesAfterThreshold(t *testing.T) {
	ctx := context.Background()
	s := newStore(t)
	super := rawPool(t)
	resetDB(t, super)
	seedUser(t, super, userA)

	const threshold = 3
	window := time.Hour

	// Below threshold: not yet suppressed.
	require.NoError(t, s.RecordChannelCaptionOutcome(ctx, userA, "chanX", false, threshold, window))
	require.NoError(t, s.RecordChannelCaptionOutcome(ctx, userA, "chanX", false, threshold, window))
	got, err := s.CaptionlessChannels(ctx, userA)
	require.NoError(t, err)
	require.NotContains(t, got, "chanX", "2 < threshold 3: not suppressed yet")

	// Crossing the threshold suppresses the channel.
	require.NoError(t, s.RecordChannelCaptionOutcome(ctx, userA, "chanX", false, threshold, window))
	got, err = s.CaptionlessChannels(ctx, userA)
	require.NoError(t, err)
	require.Contains(t, got, "chanX", "3 consecutive no-caption results suppress the channel")

	// A successful caption fetch resets it.
	require.NoError(t, s.RecordChannelCaptionOutcome(ctx, userA, "chanX", true, threshold, window))
	got, err = s.CaptionlessChannels(ctx, userA)
	require.NoError(t, err)
	require.NotContains(t, got, "chanX", "a captioned video clears suppression")
}

func TestChannelCaptionMemory_WindowExpiryReProbes(t *testing.T) {
	ctx := context.Background()
	s := newStore(t)
	super := rawPool(t)
	resetDB(t, super)
	seedUser(t, super, userA)

	// A negative window means captionless_until lands in the past — modelling an
	// elapsed suppression window, which must make the channel eligible again.
	require.NoError(t, s.RecordChannelCaptionOutcome(ctx, userA, "chanY", false, 1, -time.Hour))
	got, err := s.CaptionlessChannels(ctx, userA)
	require.NoError(t, err)
	require.NotContains(t, got, "chanY", "an expired window re-enables the channel for a re-probe")
}

func TestChannelCaptionMemory_DisabledThresholdIsNoOp(t *testing.T) {
	ctx := context.Background()
	s := newStore(t)
	super := rawPool(t)
	resetDB(t, super)
	seedUser(t, super, userA)

	require.NoError(t, s.RecordChannelCaptionOutcome(ctx, userA, "chanZ", false, 0, time.Hour))
	got, err := s.CaptionlessChannels(ctx, userA)
	require.NoError(t, err)
	require.Empty(t, got, "threshold 0 disables the memory — nothing recorded")
}
