"""Period-aware aggregation: raw per-period compounded returns + trailing rolling mean — NO normalization."""
from __future__ import annotations

from fxhnt.application.periods import aggregate


def test_daily_passthrough_with_rolling_mean() -> None:
    pts = aggregate(["2026-06-05", "2026-06-06", "2026-06-07"], [0.01, 0.02, -0.01], "daily", window=2)
    assert [p.label for p in pts] == ["2026-06-05", "2026-06-06", "2026-06-07"]
    assert abs(pts[0].ret - 0.01) < 1e-12
    assert abs(pts[1].rolling_mean - (0.01 + 0.02) / 2) < 1e-12       # trailing 2
    assert abs(pts[2].rolling_mean - (0.02 - 0.01) / 2) < 1e-12


def test_weekly_buckets_compound_within_iso_week() -> None:
    # 2026-06-05 is Fri (prior ISO week); 2026-06-08 Mon + 2026-06-09 Tue share the next ISO week
    pts = aggregate(["2026-06-05", "2026-06-08", "2026-06-09"], [0.01, 0.02, 0.03], "weekly", window=4)
    assert len(pts) == 2
    assert all(p.label.startswith("2026-W") for p in pts)
    assert pts[0].label != pts[1].label
    assert abs(pts[0].ret - 0.01) < 1e-12
    assert abs(pts[1].ret - ((1.02 * 1.03) - 1.0)) < 1e-12           # compounded within the week, raw


def test_monthly_bucket_raw_no_normalization() -> None:
    pts = aggregate(["2026-05-30", "2026-06-01", "2026-06-15"], [0.01, 0.02, 0.03], "monthly", window=4)
    assert [p.label for p in pts] == ["2026-05", "2026-06"]
    assert abs(pts[1].ret - ((1.02 * 1.03) - 1.0)) < 1e-12          # raw compounded, not z-scored


def test_unknown_period_raises() -> None:
    try:
        aggregate(["2026-06-05"], [0.01], "yearly")
        assert False, "expected ValueError"
    except ValueError:
        pass
