# #region preview_session_serializer [C:2] [TYPE Module] [SEMANTICS preview, record, serialize]
# @defgroup Translate Module group.
# @BRIEF Serialization helpers for preview sessions: record serialization.

from typing import Any

from ...models.translate import TranslationPreviewRecord


# #region serialize_preview_record [C:1] [TYPE Function] [SEMANTICS preview, record, serialize]
# @BRIEF Serialize a TranslationPreviewRecord to a response dict.
def serialize_preview_record(r: TranslationPreviewRecord) -> dict[str, Any]:
    """Serialize a TranslationPreviewRecord to a response dict."""
    return {
        "id": r.id,
        "source_sql": r.source_sql,
        "target_sql": r.target_sql,
        "source_object_type": r.source_object_type,
        "source_object_id": r.source_object_id,
        "source_object_name": r.source_object_name,
        "status": r.status,
        "feedback": r.feedback,
        "source_language_detected": (
            r.languages[0].source_language_detected
            if r.languages and r.languages[0].source_language_detected
            else None
        ),
        "needs_review": r.languages[0].needs_review if r.languages else False,
        "languages": [
            {
                "language_code": le.language_code,
                "source_language_detected": le.source_language_detected,
                "translated_value": le.translated_value,
                "final_value": le.final_value,
                "user_edit": le.user_edit,
                "status": le.status,
                "needs_review": le.needs_review,
            }
            for le in (r.languages or [])
        ] if r.languages else [],
    }
# #endregion serialize_preview_record
# #endregion preview_session_serializer
