"""Regression tests for UTC timestamp handling (Issue #206).

Ensures that date string parsing always produces UTC timestamps,
regardless of the system's local timezone.
"""

from __future__ import annotations

from datetime import UTC, datetime


def test_parse_dates_produce_utc_timestamps():
    """strptime + .replace(tzinfo=UTC) must produce UTC epoch, not local."""
    dt = datetime.strptime("2025-01-01", "%Y-%m-%d").replace(tzinfo=UTC)
    ts_ms = int(dt.timestamp() * 1000)
    # 2025-01-01 00:00:00 UTC = 1735689600000 ms
    assert ts_ms == 1735689600000, f"Expected 1735689600000, got {ts_ms}"


def test_datetime_to_start_ms_is_utc():
    """_datetime_to_start_ms must return UTC epoch milliseconds."""
    from opendeviationbar.orchestration.helpers import _datetime_to_start_ms

    dt = datetime.strptime("2025-06-15", "%Y-%m-%d").replace(tzinfo=UTC)
    ts = _datetime_to_start_ms(dt)
    expected = int(datetime(2025, 6, 15, tzinfo=UTC).timestamp() * 1000)
    assert ts == expected


def test_datetime_to_end_ms_is_utc():
    """_datetime_to_end_ms must return end-of-day UTC epoch milliseconds."""
    from opendeviationbar.orchestration.helpers import _datetime_to_end_ms

    dt = datetime.strptime("2025-06-15", "%Y-%m-%d").replace(tzinfo=UTC)
    ts = _datetime_to_end_ms(dt)
    # End of day = start + 86399 seconds
    start = int(datetime(2025, 6, 15, tzinfo=UTC).timestamp() * 1000)
    assert ts == start + 86399 * 1000


def test_fromtimestamp_with_utc():
    """datetime.fromtimestamp with tz=UTC must not shift by local offset."""
    epoch_ms = 1735689600000  # 2025-01-01 00:00:00 UTC
    dt = datetime.fromtimestamp(epoch_ms / 1000, tz=UTC)
    assert dt.year == 2025
    assert dt.month == 1
    assert dt.day == 1
    assert dt.hour == 0
