"""Sample payload for the market intel candidate queue review decision 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_inventory import (
    build_mcp_fetch_candidate_queue_writer_review_inventory_preview,
)


_SAMPLE_WRITER_REVIEW_DECISION_PACKAGE = None


def build_sample_writer_review_decision_package():
    global _SAMPLE_WRITER_REVIEW_DECISION_PACKAGE
    if _SAMPLE_WRITER_REVIEW_DECISION_PACKAGE is not None:
        return deepcopy(_SAMPLE_WRITER_REVIEW_DECISION_PACKAGE)

    inventory_preview = (
        build_mcp_fetch_candidate_queue_writer_review_inventory_preview()
    )
    inventory_package = inventory_preview["sample_writer_review_inventory_package"]
    inventory_result = build_mcp_fetch_candidate_queue_writer_review_inventory_preview(
        writer_review_handoff_package=inventory_package[
            "writer_review_handoff_package"
        ],
        writer_review_handoff_result=inventory_package[
            "writer_review_handoff_result"
        ],
        operator_review_inventory=inventory_package["operator_review_inventory"],
    )
    inventory = inventory_result["operator_review_inventory_summary"]
    dedupe_keys = inventory["sample_dedupe_keys"]
    decision_rows = []
    for index, dedupe_key in enumerate(dedupe_keys):
        proposed_decision = "matched" if index == 0 else "low_score"
        decision_rows.append(
            {
                "dedupe_key": dedupe_key,
                "current_review_state": "needs_review",
                "proposed_review_decision": proposed_decision,
                "decision_bucket": "recoverable_low_score",
                "evidence_lane": "exact_identity",
                "evidence_ref": (
                    ARTIFACT_PREFIX
                    + f"candidate-queue-review-decision-evidence-{index + 1}.json"
                ),
                "decision_notes": (
                    "same candidate lane checked; keep human audit trail"
                ),
                "hard_veto_present": False,
                "stronger_existing_match_conflict": False,
                "false_positive_guard_passed": True,
                "variant_conflict_checked": True,
                "overwrite_protection_checked": True,
                "exact_identity_confirmed": proposed_decision == "matched",
            }
        )

    operator_review_decision = {
        "review_decision_id": (
            "market-intel-candidate-writer-review-decision-sample"
        ),
        "review_inventory_id": inventory["review_inventory_id"],
        "handoff_id": inventory["handoff_id"],
        "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,
        "decision_scope": "candidate_queue_review_decision",
        "decision_mode": "human_review_only",
        "expected_current_review_state": "needs_review",
        "decision_row_count": len(decision_rows),
        "expected_decision_row_count": inventory["dedupe_key_count"],
        "expected_dedupe_keys": dedupe_keys,
        "review_queue_artifact_path": inventory["review_queue_artifact_path"],
        "review_inventory_artifact_path": (
            inventory["review_inventory_artifact_path"]
        ),
        "review_decision_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-decision-sample.json"
        ),
        "decision_rows": decision_rows,
        "operator_confirmations": {
            "review_inventory_checked": True,
            "candidate_queue_review_only": True,
            "human_decision_only": True,
            "manual_record_required": 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,
            "stronger_existing_match_guard_preserved": True,
            "false_positive_guard_completed": True,
            "variant_sensitive_review_completed": True,
            "hard_veto_respected": True,
        },
        "api_execution_allowed": False,
        "real_write_allowed_by_api": False,
        "api_candidate_review_allowed": False,
        "api_updates_review_state": False,
        "api_writes_match_result": False,
    }
    _SAMPLE_WRITER_REVIEW_DECISION_PACKAGE = {
        "writer_review_inventory_package": inventory_package,
        "writer_review_inventory_result": inventory_result,
        "operator_review_decision": operator_review_decision,
    }
    return deepcopy(_SAMPLE_WRITER_REVIEW_DECISION_PACKAGE)
