from __future__ import annotations

from datetime import datetime, timezone


def utc_now() -> datetime:
    return datetime.now(timezone.utc)


def ensure_aware_utc(value: datetime | None) -> datetime | None:
    """Return a timezone-aware UTC datetime.

    SQLite and some DB drivers may return naive datetimes even when SQLAlchemy
    columns are declared as DateTime(timezone=True). Treat naive values as UTC
    so comparisons with aware `datetime.now(timezone.utc)` do not crash.
    """
    if value is None:
        return None
    if value.tzinfo is None:
        return value.replace(tzinfo=timezone.utc)
    return value.astimezone(timezone.utc)
