package usecase

import (
	"context"
	"testing"

	"apps/backend/internal/module/radar/domain"
)

func TestPauseProductWatchesIsIdempotentAndPreservesHistory(t *testing.T) {
	ctx := context.Background()
	svc, _ := productWatchService()
	active, err := svc.CreateWatch(ctx, 42, WatchInput{Terms: []string{"敏感肌"}, Enabled: true, BrandID: "b1", ProductID: "p1"})
	if err != nil {
		t.Fatal(err)
	}
	archived, err := svc.CreateWatch(ctx, 42, WatchInput{Terms: []string{"敏感肌"}, Enabled: false, BrandID: "b1", ProductID: "p1"})
	if err != nil {
		t.Fatal(err)
	}
	if err := svc.ArchiveWatch(ctx, 42, archived.ID); err != nil {
		t.Fatal(err)
	}
	count, err := svc.PauseProductWatches(ctx, 42, "p1")
	if err != nil || count != 1 {
		t.Fatalf("pause count=%d err=%v", count, err)
	}
	count, err = svc.PauseProductWatches(ctx, 42, "p1")
	if err != nil || count != 0 {
		t.Fatalf("retry count=%d err=%v", count, err)
	}
	got, _ := svc.GetWatch(ctx, 42, active.ID)
	if got.Status != domain.WatchPaused || got.PauseReason != domain.PauseReasonProductUnavailable {
		t.Fatalf("active history not paused: %+v", got)
	}
	got, _ = svc.GetWatch(ctx, 42, archived.ID)
	if got.Status != domain.WatchArchived {
		t.Fatalf("archived history changed: %+v", got)
	}
}
