"""Sample payload for the market intel candidate queue review inventory 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_review_handoff import (
    build_mcp_fetch_candidate_queue_writer_review_handoff_preview,
)


_SAMPLE_WRITER_REVIEW_INVENTORY_PACKAGE = None


def build_sample_writer_review_inventory_package():
    global _SAMPLE_WRITER_REVIEW_INVENTORY_PACKAGE
    if _SAMPLE_WRITER_REVIEW_INVENTORY_PACKAGE is not None:
        return deepcopy(_SAMPLE_WRITER_REVIEW_INVENTORY_PACKAGE)

    handoff_preview = build_mcp_fetch_candidate_queue_writer_review_handoff_preview()
    handoff_package = handoff_preview["sample_writer_review_handoff_package"]
    handoff_result = build_mcp_fetch_candidate_queue_writer_review_handoff_preview(
        writer_post_closeout_inventory_review_package=handoff_package[
            "writer_post_closeout_inventory_review_package"
        ],
        writer_post_closeout_inventory_review_result=handoff_package[
            "writer_post_closeout_inventory_review_result"
        ],
        operator_handoff=handoff_package["operator_handoff"],
    )
    handoff = handoff_result["operator_handoff_summary"]
    row_count = max(0, int(handoff["payload_row_count"] or 0))
    sample_dedupe_keys = [
        f"mcp-fetch-candidate-review-{index + 1:03d}"
        for index in range(row_count)
    ]
    operator_review_inventory = {
        "review_inventory_id": (
            "market-intel-candidate-writer-review-inventory-sample"
        ),
        "handoff_id": handoff["handoff_id"],
        "inventory_review_id": handoff["inventory_review_id"],
        "closeout_id": handoff["closeout_id"],
        "run_package_id": handoff["run_package_id"],
        "receipt_id": handoff["receipt_id"],
        "target_table": TARGET_TABLE,
        "payload_row_count": handoff["payload_row_count"],
        "expected_payload_row_count": handoff["expected_payload_row_count"],
        "found_row_count": row_count,
        "missing_row_count": 0,
        "unexpected_duplicate_count": 0,
        "dedupe_key_count": row_count,
        "sample_dedupe_keys": sample_dedupe_keys,
        "review_state": "needs_review",
        "review_state_breakdown": {"needs_review": row_count},
        "review_scope": "candidate_queue_review",
        "next_queue": "manual_candidate_queue_review",
        "review_queue_artifact_path": handoff["review_queue_artifact_path"],
        "handoff_artifact_path": handoff["handoff_artifact_path"],
        "inventory_artifact_path": handoff["inventory_artifact_path"],
        "review_inventory_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-inventory-sample.json"
        ),
        "read_only_query_result_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-inventory-read-only-query-sample.json"
        ),
        "query_summary": {
            "query_type": "read_only_select",
            "read_only_query_executed_by_operator": True,
            "target_table": TARGET_TABLE,
            "expected_rows_found": True,
            "row_count_matches_handoff": True,
            "review_state_only_needs_review": True,
            "no_missing_dedupe_keys": True,
            "no_unexpected_duplicates": True,
        },
        "operator_confirmations": {
            "handoff_reviewed": True,
            "review_inventory_read_only": True,
            "candidate_queue_review_only": True,
            "manual_review_required": True,
            "no_missing_rows": True,
            "no_unexpected_duplicates": True,
            "no_approval_token_payload": True,
            "no_api_cli_execution": True,
            "no_api_database_write": True,
            "no_api_inventory_query": True,
            "no_api_review_state_update": True,
            "no_api_queue_insert": True,
            "no_api_file_write": True,
            "no_scheduler_attach": True,
        },
        "api_execution_allowed": False,
        "real_write_allowed_by_api": False,
        "api_candidate_review_allowed": False,
        "api_updates_review_state": False,
    }
    _SAMPLE_WRITER_REVIEW_INVENTORY_PACKAGE = {
        "writer_review_handoff_package": handoff_package,
        "writer_review_handoff_result": handoff_result,
        "operator_review_inventory": operator_review_inventory,
    }
    return deepcopy(_SAMPLE_WRITER_REVIEW_INVENTORY_PACKAGE)
