"""Preview assembly helpers for seller quote revisions."""

from __future__ import annotations

from typing import Any, Callable


def build_quote_preview_evidence_rows(
    *,
    intent: Any,
    field_mapping: Any,
    field_preview: Any,
    preview_readiness: Any | None,
    preview_readiness_error: str | None,
    current_value_load_error: str | None,
    evidence_factory: Callable[..., Any],
) -> list[Any]:
    evidence = [
        evidence_factory(
            source_type="quote_intent",
            label="Requested Change",
            detail=f"{intent.target_label} preview normalized `{intent.requested_value_raw}` to `{intent.requested_value_display}`.",
        ),
        evidence_factory(
            source_type="field_mapping",
            label="Resolved Quote Field",
            detail=(
                f"Mapped `{intent.semantic_key}` to `{field_preview.object_name}.{field_preview.field_name}` "
                f"(confidence {field_mapping.confidence:.2f})."
            ),
        ),
    ]
    if current_value_load_error:
        evidence.append(
            evidence_factory(
                source_type="quote_field_read",
                label="Current Quote Value",
                detail=f"Current field value could not be loaded: {current_value_load_error}",
            )
        )
    else:
        evidence.append(
            evidence_factory(
                source_type="quote_field_read",
                label="Current Quote Value",
                detail=(
                    f"Gremlin read `{field_preview.object_name}.{field_preview.field_name}` as "
                    f"`{field_preview.current_value_display}` on the selected quote."
                ),
            )
        )
    if preview_readiness is not None:
        evidence.append(
            evidence_factory(
                source_type="preview_readiness",
                label="Readiness Preview",
                detail=(
                    f"Preview quote-readiness mode is `{preview_readiness.readiness_mode}` with "
                    f"{len(preview_readiness.issues)} issue(s) and "
                    f"{len(preview_readiness.recommended_approval_roles)} recommended approver role(s)."
                ),
            )
        )
    elif preview_readiness_error:
        evidence.append(
            evidence_factory(
                source_type="preview_readiness",
                label="Readiness Preview",
                detail=f"Preview readiness evaluation was unavailable: {preview_readiness_error}",
            )
        )
    return evidence


def build_quote_revision_preview_payload(
    *,
    intent: Any,
    quote_status: Any,
    context_id: str | None,
    readiness_mode: str,
    status: str,
    support_reason: str = "",
    capability_gaps: list[str] | None = None,
    field_preview: Any | None = None,
    line_item_preview: Any | None = None,
    preview_readiness_mode: str | None = None,
    preview_policy_state: str = "review_required",
    preview_policy_reasons: list[str] | None = None,
    recommended_approval_roles: list[str] | None = None,
    apply_supported: bool = True,
    apply_support_reason: str = "",
    evidence: list[Any] | None = None,
    next_step: str = "",
) -> dict[str, Any]:
    return {
        "opportunity_id": intent.opportunity_id,
        "crm_org_id": intent.crm_org_id,
        "org_alias": intent.org_alias,
        "context_id": context_id,
        "status": status,
        "detected_engine": quote_status.detected_engine,
        "support_reason": support_reason,
        "capability_gaps": list(capability_gaps or []),
        "readiness_mode": readiness_mode,
        "intent": intent,
        "selected_quote": quote_status.selected_quote,
        "field_preview": field_preview,
        "line_item_preview": line_item_preview,
        "approval_state": quote_status.approval_state,
        "preview_readiness_mode": preview_readiness_mode or readiness_mode,
        "preview_policy_state": preview_policy_state,
        "preview_policy_reasons": list(preview_policy_reasons or []),
        "matched_approval_route_names": list(quote_status.matched_approval_route_names),
        "recommended_approval_roles": list(recommended_approval_roles or []),
        "apply_supported": bool(apply_supported),
        "apply_support_reason": apply_support_reason,
        "evidence": list(evidence or []),
        "next_step": next_step,
    }
