"""Approval-context translation helpers for seller quote revisions."""

from __future__ import annotations

from typing import Any, Callable


def _jsonable(value: Any) -> Any:
    if hasattr(value, "to_dict"):
        try:
            return _jsonable(value.to_dict())
        except Exception:
            pass
    if isinstance(value, dict):
        return {str(key): _jsonable(item) for key, item in value.items()}
    if isinstance(value, list):
        return [_jsonable(item) for item in value]
    return value


def build_quote_revision_approval_context_payload(
    *,
    run_id: str,
    preview: Any,
    seller_slack_target: Any | None = None,
) -> dict[str, Any] | None:
    if preview.selected_quote is None or (
        preview.field_preview is None and preview.line_item_preview is None
    ):
        return None
    return _jsonable(
        {
        "run_id": run_id,
        "opportunity_id": preview.opportunity_id,
        "crm_org_id": preview.crm_org_id,
        "org_alias": preview.org_alias,
        "detected_engine": preview.detected_engine,
        "intent": preview.intent,
        "selected_quote": preview.selected_quote,
        "field_preview": preview.field_preview,
        "line_item_preview": preview.line_item_preview,
        "preview_policy_state": preview.preview_policy_state,
        "preview_policy_reasons": list(preview.preview_policy_reasons or []),
        "matched_approval_route_names": list(preview.matched_approval_route_names or []),
        "recommended_approval_roles": list(preview.recommended_approval_roles or []),
        "evidence": list(preview.evidence or []),
        "seller_slack_target": seller_slack_target,
        }
    )


def materialize_quote_revision_approval_context(
    *,
    payload: dict[str, Any] | None,
    validator: Callable[[dict[str, Any]], Any],
) -> Any | None:
    if payload is None:
        return None
    try:
        return validator(payload)
    except Exception:
        return None


def build_quote_revision_preview_payload_from_approval_context(
    *,
    approval_context: Any,
) -> dict[str, Any]:
    return {
        "opportunity_id": approval_context.opportunity_id,
        "crm_org_id": approval_context.crm_org_id,
        "org_alias": approval_context.org_alias,
        "detected_engine": str(approval_context.detected_engine or "unknown"),
        "status": "preview_ready",
        "support_reason": "",
        "readiness_mode": "ready",
        "intent": approval_context.intent,
        "selected_quote": approval_context.selected_quote,
        "field_preview": approval_context.field_preview,
        "line_item_preview": approval_context.line_item_preview,
        "preview_policy_state": approval_context.preview_policy_state,
        "preview_policy_reasons": list(approval_context.preview_policy_reasons or []),
        "matched_approval_route_names": list(approval_context.matched_approval_route_names or []),
        "recommended_approval_roles": list(approval_context.recommended_approval_roles or []),
        "evidence": list(approval_context.evidence or []),
    }
