package repository

import (
	"testing"
	"time"

	"haixun-backend/internal/model/scan_post/domain/entity"

	"go.mongodb.org/mongo-driver/bson"
)

func TestMatchesRecent7dPost_Includes7dWindowWithoutDate(t *testing.T) {
	now := time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC)
	item := entity.ScanPost{RecencyDays: 7}
	if !matchesRecent7dPost(item, now) {
		t.Fatal("expected 7d-window post without date to match recent_7d filter")
	}
}

func TestMatchesRecent7dPost_Excludes14dWindowWithoutDate(t *testing.T) {
	now := time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC)
	item := entity.ScanPost{RecencyDays: 14, QueryDimension: "recency"}
	if matchesRecent7dPost(item, now) {
		t.Fatal("expected 14d-window post without date to be excluded from recent_7d")
	}
}

func TestMatchesRecent7dPost_IncludesPostedWithin7Days(t *testing.T) {
	now := time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC)
	item := entity.ScanPost{
		Priority: "relevant",
		PostedAt: "2026-06-28",
	}
	if !matchesRecent7dPost(item, now) {
		t.Fatal("expected post within 7 days to match")
	}
}

func TestMatchesRecent7dPost_ExcludesOldRelevantWithoutDate(t *testing.T) {
	now := time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC)
	item := entity.ScanPost{Priority: "relevant", QueryDimension: "relevance"}
	if matchesRecent7dPost(item, now) {
		t.Fatal("expected old relevance-only post without date to be excluded")
	}
}

func TestRecent7dListQuery_HasExpectedClauses(t *testing.T) {
	now := time.Date(2026, 6, 30, 12, 0, 0, 0, time.UTC)
	query := recent7dListQuery(now)
	orClause, ok := query["$or"].([]bson.M)
	if !ok || len(orClause) != 2 {
		t.Fatalf("expected 2 OR branches, got %T %v", query["$or"], query["$or"])
	}
}
