package repository

import (
	"context"
	"testing"

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

func TestMemoryWatchContextFiltersAndCopy(t *testing.T) {
	m := NewMemory()
	legacy := &domain.RadarWatch{ID: "legacy", OwnerUID: 1, Terms: []string{"找水電"}, Status: domain.WatchActive, CreatedAt: 1, UpdatedAt: 1}
	product := &domain.RadarWatch{ID: "product", OwnerUID: 1, Terms: []string{"找抓漏"}, Status: domain.WatchActive, ContextMode: domain.WatchContextProduct, BrandID: "b1", ProductID: "p1", CreatedAt: 2, UpdatedAt: 2}
	if err := m.SaveWatch(context.Background(), legacy); err != nil {
		t.Fatal(err)
	}
	if err := m.SaveWatch(context.Background(), product); err != nil {
		t.Fatal(err)
	}
	rows, total, err := m.ListWatches(context.Background(), 1, domain.WatchListFilter{ContextMode: domain.WatchContextGeneric})
	if err != nil || total != 1 || len(rows) != 1 || rows[0].ContextMode != domain.WatchContextGeneric {
		t.Fatalf("legacy filter: total=%d rows=%#v err=%v", total, rows, err)
	}
	rows, total, err = m.ListWatches(context.Background(), 1, domain.WatchListFilter{BrandID: "b1", ProductID: "p1"})
	if err != nil || total != 1 || len(rows) != 1 {
		t.Fatalf("product filter: total=%d rows=%#v err=%v", total, rows, err)
	}
	rows[0].Terms[0] = "mutated"
	got, err := m.GetWatch(context.Background(), "product")
	if err != nil || got.Terms[0] == "mutated" {
		t.Fatalf("repository leaked mutable slice: %#v %v", got, err)
	}
}
