#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""市場情報 report input 後續 POST API。"""

from flask import jsonify, request

from auth import login_required
from routes.market_intel_review_post_ai_routes import (
    _build_ai_summary_persistence_telegram_dispatch_archive,
)
from routes.market_intel_review_routes import _extract_run_payload, market_intel_review_bp
from services.market_intel import MarketIntelService
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_handoff import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_handoff,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_index import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_index,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_write import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_write,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness,
)
from services.market_intel.candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_receipt import (
    build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_receipt,
)


def _as_dict(value):
    return value if isinstance(value, dict) else {}


def _build_ai_summary_persistence_telegram_dispatch_archive_summary(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    archive = _build_ai_summary_persistence_telegram_dispatch_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_archive_summary(
        telegram_dispatch_archive=archive,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_input(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    archive_summary = _build_ai_summary_persistence_telegram_dispatch_archive_summary(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input(
        telegram_dispatch_archive_summary=archive_summary,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_run_package(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_input = _build_ai_summary_persistence_telegram_dispatch_report_input(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package(
        telegram_dispatch_report_input=report_input,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_run_readiness(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_run_package = (
        _build_ai_summary_persistence_telegram_dispatch_report_run_package(
            service=service,
            sample_result=sample_result,
            operator_evidence=operator_evidence,
            writer_output=writer_output,
            smoke_result=smoke_result,
            payload_error=payload_error,
            limit=limit,
            execute_requested=execute_requested,
            apply_real_write=apply_real_write,
        )
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness(
        telegram_dispatch_report_run_package=report_run_package,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_run_receipt_payload(sample_result, operator_evidence):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    return _as_dict(
        operator_evidence.get("market_intel_report_run_receipt")
        or operator_evidence.get("telegram_dispatch_report_run_receipt")
        or operator_evidence.get("report_generation_receipt")
        or operator_evidence.get("report_run_receipt")
        or sample_result.get("market_intel_report_run_receipt")
        or sample_result.get("telegram_dispatch_report_run_receipt")
        or sample_result.get("report_generation_receipt")
        or sample_result.get("report_run_receipt")
    )


def _build_ai_summary_persistence_telegram_dispatch_report_run_receipt(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_run_readiness = (
        _build_ai_summary_persistence_telegram_dispatch_report_run_readiness(
            service=service,
            sample_result=sample_result,
            operator_evidence=operator_evidence,
            writer_output=writer_output,
            smoke_result=smoke_result,
            payload_error=payload_error,
            limit=limit,
            execute_requested=execute_requested,
            apply_real_write=apply_real_write,
        )
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_receipt(
        telegram_dispatch_report_run_readiness=report_run_readiness,
        report_run_receipt=_extract_report_run_receipt_payload(
            sample_result,
            operator_evidence,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_closeout(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_run_receipt = _build_ai_summary_persistence_telegram_dispatch_report_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout(
        telegram_dispatch_report_run_receipt=report_run_receipt,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_archive(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_closeout = _build_ai_summary_persistence_telegram_dispatch_report_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive(
        telegram_dispatch_report_closeout=report_closeout,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_archive_summary(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_archive = _build_ai_summary_persistence_telegram_dispatch_report_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary(
        telegram_dispatch_report_archive=report_archive,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_handoff(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_archive_summary = _build_ai_summary_persistence_telegram_dispatch_report_archive_summary(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_handoff(
        telegram_dispatch_report_archive_summary=report_archive_summary,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_index(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_handoff = _build_ai_summary_persistence_telegram_dispatch_report_catalog_handoff(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_index(
        telegram_dispatch_report_catalog_handoff=report_catalog_handoff,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_index = _build_ai_summary_persistence_telegram_dispatch_report_catalog_index(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight(
        telegram_dispatch_report_catalog_index=report_catalog_index,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_write(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_write_preflight = _build_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_write(
        telegram_dispatch_report_catalog_write_preflight=report_catalog_write_preflight,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_write = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_write(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package(
        telegram_dispatch_report_catalog_record_write=report_catalog_record_write,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_run_package = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness(
        telegram_dispatch_report_catalog_record_run_package=report_catalog_record_run_package,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_catalog_record_run_receipt_payload(
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    payload = _as_dict(
        operator_evidence.get("market_intel_report_catalog_record_run_receipt")
        or operator_evidence.get("telegram_dispatch_report_catalog_record_run_receipt")
        or operator_evidence.get("report_catalog_record_run_receipt")
        or operator_evidence.get("catalog_record_run_receipt")
        or sample_result.get("market_intel_report_catalog_record_run_receipt")
        or sample_result.get("telegram_dispatch_report_catalog_record_run_receipt")
        or sample_result.get("report_catalog_record_run_receipt")
        or sample_result.get("catalog_record_run_receipt")
    )
    if payload:
        return payload
    writer_output = _as_dict(writer_output)
    smoke_result = _as_dict(smoke_result)
    if writer_output or smoke_result:
        return {
            "mode": "manual_market_intel_report_catalog_record_run_receipt",
            "writer_output": writer_output,
            "postwrite_smoke_result": smoke_result,
        }
    return {}


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_run_readiness = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt(
        telegram_dispatch_report_catalog_record_run_readiness=report_catalog_record_run_readiness,
        catalog_record_run_receipt=_extract_report_catalog_record_run_receipt_payload(
            sample_result,
            operator_evidence,
            writer_output,
            smoke_result,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_catalog_record_commit_payload(sample_result, operator_evidence):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    return _as_dict(
        operator_evidence.get("market_intel_report_catalog_record_commit")
        or operator_evidence.get("telegram_dispatch_report_catalog_record_commit")
        or operator_evidence.get("report_catalog_record_commit")
        or operator_evidence.get("catalog_record_commit")
        or sample_result.get("market_intel_report_catalog_record_commit")
        or sample_result.get("telegram_dispatch_report_catalog_record_commit")
        or sample_result.get("report_catalog_record_commit")
        or sample_result.get("catalog_record_commit")
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_run_receipt = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit(
        telegram_dispatch_report_catalog_record_run_receipt=report_catalog_record_run_receipt,
        report_catalog_record_commit=_extract_report_catalog_record_commit_payload(
            sample_result,
            operator_evidence,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_catalog_record_closeout_payload(sample_result, operator_evidence):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    return _as_dict(
        operator_evidence.get("market_intel_report_catalog_record_closeout")
        or operator_evidence.get("telegram_dispatch_report_catalog_record_closeout")
        or operator_evidence.get("report_catalog_record_closeout")
        or operator_evidence.get("catalog_record_closeout")
        or sample_result.get("market_intel_report_catalog_record_closeout")
        or sample_result.get("telegram_dispatch_report_catalog_record_closeout")
        or sample_result.get("report_catalog_record_closeout")
        or sample_result.get("catalog_record_closeout")
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_commit = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout(
        telegram_dispatch_report_catalog_record_commit=report_catalog_record_commit,
        report_catalog_record_closeout=_extract_report_catalog_record_closeout_payload(
            sample_result,
            operator_evidence,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_catalog_record_archive_payload(sample_result, operator_evidence):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    return _as_dict(
        operator_evidence.get("market_intel_report_catalog_record_archive")
        or operator_evidence.get("telegram_dispatch_report_catalog_record_archive")
        or operator_evidence.get("report_catalog_record_archive")
        or operator_evidence.get("catalog_record_archive")
        or sample_result.get("market_intel_report_catalog_record_archive")
        or sample_result.get("telegram_dispatch_report_catalog_record_archive")
        or sample_result.get("report_catalog_record_archive")
        or sample_result.get("catalog_record_archive")
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_closeout = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive(
        telegram_dispatch_report_catalog_record_closeout=report_catalog_record_closeout,
        report_catalog_record_archive=_extract_report_catalog_record_archive_payload(
            sample_result,
            operator_evidence,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_catalog_record_archive_summary_payload(
    sample_result,
    operator_evidence,
):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    return _as_dict(
        operator_evidence.get("market_intel_report_catalog_record_archive_summary")
        or operator_evidence.get(
            "telegram_dispatch_report_catalog_record_archive_summary"
        )
        or operator_evidence.get("report_catalog_record_archive_summary")
        or operator_evidence.get("catalog_record_archive_summary")
        or sample_result.get("market_intel_report_catalog_record_archive_summary")
        or sample_result.get("telegram_dispatch_report_catalog_record_archive_summary")
        or sample_result.get("report_catalog_record_archive_summary")
        or sample_result.get("catalog_record_archive_summary")
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_archive = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary(
        telegram_dispatch_report_catalog_record_archive=report_catalog_record_archive,
        report_catalog_record_archive_summary=_extract_report_catalog_record_archive_summary_payload(
            sample_result,
            operator_evidence,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


def _extract_report_catalog_record_final_closeout_payload(
    sample_result,
    operator_evidence,
):
    operator_evidence = _as_dict(operator_evidence)
    sample_result = _as_dict(sample_result)
    return _as_dict(
        operator_evidence.get("market_intel_report_catalog_record_final_closeout")
        or operator_evidence.get(
            "telegram_dispatch_report_catalog_record_final_closeout"
        )
        or operator_evidence.get("report_catalog_record_final_closeout")
        or operator_evidence.get("catalog_record_final_closeout")
        or sample_result.get("market_intel_report_catalog_record_final_closeout")
        or sample_result.get("telegram_dispatch_report_catalog_record_final_closeout")
        or sample_result.get("report_catalog_record_final_closeout")
        or sample_result.get("catalog_record_final_closeout")
    )


def _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout(
    *,
    service,
    sample_result,
    operator_evidence,
    writer_output,
    smoke_result,
    payload_error,
    limit,
    execute_requested,
    apply_real_write,
):
    report_catalog_record_archive_summary = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    return build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout(
        telegram_dispatch_report_catalog_record_archive_summary=report_catalog_record_archive_summary,
        report_catalog_record_final_closeout=_extract_report_catalog_record_final_closeout_payload(
            sample_result,
            operator_evidence,
        ),
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    archive_summary = _build_ai_summary_persistence_telegram_dispatch_archive_summary(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_input(
        telegram_dispatch_archive_summary=archive_summary,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    report_input = _build_ai_summary_persistence_telegram_dispatch_report_input(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data = build_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_package(
        telegram_dispatch_report_input=report_input,
        operator_evidence=operator_evidence,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_readiness():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_receipt",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_run_receipt():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_closeout():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_archive_summary():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_archive_summary(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_handoff",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_handoff():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_handoff(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_index",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_index():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_index(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_write_preflight(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_write",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_write():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_write(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_package(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_readiness(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_run_receipt(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_commit(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_archive_summary(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200


@market_intel_review_bp.route(
    "/api/market_intel/manual_sample_review/"
    "candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout",
    methods=["POST"],
)
@login_required
def market_intel_manual_sample_candidate_queue_review_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout():
    service = MarketIntelService()
    execute_requested = request.args.get("execute", "false").lower() == "true"
    apply_real_write = request.args.get("apply_real_write", "false").lower() == "true"
    sample_result, operator_evidence, writer_output, smoke_result, payload_error, limit = (
        _extract_run_payload()
    )
    data = _build_ai_summary_persistence_telegram_dispatch_report_catalog_record_final_closeout(
        service=service,
        sample_result=sample_result,
        operator_evidence=operator_evidence,
        writer_output=writer_output,
        smoke_result=smoke_result,
        payload_error=payload_error,
        limit=limit,
        execute_requested=execute_requested,
        apply_real_write=apply_real_write,
    )
    data["phase"] = service.phase
    return jsonify(data), 400 if payload_error else 200
