"""
Shared database utility functions for Compliance API routes.

Provides common helpers used across multiple route files:
- row_to_dict: Convert SQLAlchemy Row to JSON-safe dict
"""

from datetime import datetime, date
from typing import Any, Dict


def row_to_dict(row) -> Dict[str, Any]:
    """Convert a SQLAlchemy Row/RowMapping to a JSON-serializable dict.

    Handles datetime serialization and non-standard types.
    """
    result = dict(row._mapping)
    for key, val in result.items():
        if isinstance(val, datetime):
            result[key] = val.isoformat()
        elif isinstance(val, date):
            result[key] = val.isoformat()
        elif hasattr(val, '__str__') and not isinstance(val, (str, int, float, bool, list, dict, type(None))):
            result[key] = str(val)
    return result
