package domain

import (
	"testing"
	"time"
)

func TestLocalDayBoundsUsesTaipeiNotUTC(t *testing.T) {
	// 2026-08-13 02:44 UTC is 10:44 in Taipei, same local calendar day.
	// 2026-08-13 00:30 Taipei is still 2026-08-12 16:30 UTC.
	morningTaipei := time.Date(2026, 8, 13, 0, 30, 0, 0, DisplayLocation()).UnixNano()
	start, end := LocalDayBounds(morningTaipei, DisplayLocation())
	utcStart, utcEnd := UTCDayBounds(morningTaipei)
	if start == utcStart {
		t.Fatalf("local day must not collapse to UTC day: local=%d utc=%d", start, utcStart)
	}
	if morningTaipei < start || morningTaipei >= end {
		t.Fatalf("Taipei 00:30 should sit inside local today [%d, %d)", start, end)
	}
	if utcEnd <= start {
		t.Fatal("expected UTC Aug 12 window to end at Taipei midnight Aug 13")
	}
}

func TestInInboxTimeScopeKeepsResweptAndUndatedRows(t *testing.T) {
	from, to := LocalDayBounds(NowNano(), DisplayLocation())
	oldPost := from - int64(48*time.Hour)
	if InInboxTimeScope(oldPost, 0, oldPost, from, to) {
		t.Fatal("stale post that was not rematched today should stay out of today")
	}
	if !InInboxTimeScope(oldPost, from+1, oldPost, from, to) {
		t.Fatal("a post rematched today must remain visible after patrol")
	}
	if !InInboxTimeScope(0, 0, from+1, from, to) {
		t.Fatal("undated post created today must remain visible after patrol")
	}
	if !InInboxTimeScope(oldPost, 0, from+1, from, to) {
		t.Fatal("older post created today without last_matched must remain visible (no-backfill)")
	}
	if !InInboxTimeScope(from+1, 0, oldPost, from, to) {
		t.Fatal("post published today should stay on today even if created earlier")
	}
}
