"""Sample payload for the market intel candidate queue review handoff gate."""

from copy import deepcopy

from services.market_intel.mcp_fetch_candidate_queue_writer_preflight import TARGET_TABLE
from services.market_intel.mcp_fetch_candidate_queue_writer_run_readiness import (
    ARTIFACT_PREFIX,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_post_closeout_inventory_review import (
    build_mcp_fetch_candidate_queue_writer_post_closeout_inventory_review_preview,
)


_SAMPLE_WRITER_REVIEW_HANDOFF_PACKAGE = None


def build_sample_writer_review_handoff_package():
    global _SAMPLE_WRITER_REVIEW_HANDOFF_PACKAGE
    if _SAMPLE_WRITER_REVIEW_HANDOFF_PACKAGE is not None:
        return deepcopy(_SAMPLE_WRITER_REVIEW_HANDOFF_PACKAGE)

    inventory_preview = (
        build_mcp_fetch_candidate_queue_writer_post_closeout_inventory_review_preview()
    )
    inventory_package = inventory_preview[
        "sample_writer_post_closeout_inventory_review_package"
    ]
    inventory_result = (
        build_mcp_fetch_candidate_queue_writer_post_closeout_inventory_review_preview(
            writer_run_closeout_review_package=inventory_package[
                "writer_run_closeout_review_package"
            ],
            writer_run_closeout_review_result=inventory_package[
                "writer_run_closeout_review_result"
            ],
            operator_inventory=inventory_package["operator_inventory"],
        )
    )
    inventory = inventory_result["operator_inventory_summary"]
    operator_handoff = {
        "handoff_id": "market-intel-candidate-writer-review-handoff-sample",
        "inventory_review_id": inventory["inventory_review_id"],
        "closeout_id": inventory["closeout_id"],
        "run_package_id": inventory["run_package_id"],
        "receipt_id": inventory["receipt_id"],
        "target_table": TARGET_TABLE,
        "payload_row_count": inventory["payload_row_count"],
        "expected_payload_row_count": inventory["payload_row_count"],
        "expected_review_state": "needs_review",
        "review_scope": "candidate_queue_review",
        "next_queue": "manual_candidate_queue_review",
        "review_queue_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-handoff-review-queue-sample.json"
        ),
        "inventory_artifact_path": inventory["inventory_artifact_path"],
        "handoff_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-handoff-sample.json"
        ),
        "review_contract": {
            "expected_review_state": "needs_review",
            "required_columns": [
                "dedupe_key",
                "review_state",
                "momo_product_id",
                "pchome_product_id",
            ],
            "allowed_manual_actions": [
                "mark_matched",
                "mark_low_score",
                "mark_identity_veto",
                "request_fresh_search",
            ],
            "forbidden_api_actions": [
                "update_review_state",
                "insert_missing_queue_row",
                "execute_matching_decision",
            ],
        },
        "operator_confirmations": {
            "inventory_reviewed": True,
            "inventory_artifacts_preserved": True,
            "candidate_queue_review_only": True,
            "manual_review_only": True,
            "manual_review_required": True,
            "no_approval_token_payload": True,
            "no_api_cli_execution": True,
            "no_api_database_write": True,
            "no_api_inventory_query": True,
            "no_api_file_write": True,
            "no_api_review_state_update": True,
            "no_api_queue_insert": True,
            "no_scheduler_attach": True,
        },
        "api_execution_allowed": False,
        "real_write_allowed_by_api": False,
        "api_candidate_review_allowed": False,
    }
    _SAMPLE_WRITER_REVIEW_HANDOFF_PACKAGE = {
        "writer_post_closeout_inventory_review_package": inventory_package,
        "writer_post_closeout_inventory_review_result": inventory_result,
        "operator_handoff": operator_handoff,
    }
    return deepcopy(_SAMPLE_WRITER_REVIEW_HANDOFF_PACKAGE)
