"""Sample payload for the market intel review decision approval 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_decision import (
    build_mcp_fetch_candidate_queue_writer_review_decision_preview,
)


_SAMPLE_WRITER_REVIEW_DECISION_APPROVAL_PACKAGE = None


def build_sample_writer_review_decision_approval_package():
    global _SAMPLE_WRITER_REVIEW_DECISION_APPROVAL_PACKAGE
    if _SAMPLE_WRITER_REVIEW_DECISION_APPROVAL_PACKAGE is not None:
        return deepcopy(_SAMPLE_WRITER_REVIEW_DECISION_APPROVAL_PACKAGE)

    decision_preview = (
        build_mcp_fetch_candidate_queue_writer_review_decision_preview()
    )
    decision_package = decision_preview["sample_writer_review_decision_package"]
    decision_result = build_mcp_fetch_candidate_queue_writer_review_decision_preview(
        writer_review_inventory_package=decision_package[
            "writer_review_inventory_package"
        ],
        writer_review_inventory_result=decision_package[
            "writer_review_inventory_result"
        ],
        operator_review_decision=decision_package["operator_review_decision"],
    )
    decision = decision_result["operator_review_decision_summary"]
    inventory = decision_result["writer_review_inventory_summary"]
    approval_rows = []
    for row in decision["decision_rows"]:
        approval_rows.append(
            {
                "dedupe_key": row["dedupe_key"],
                "current_review_state": row["current_review_state"],
                "approval_result": "approved_for_writer_preflight",
                "proposed_review_decision": row["proposed_review_decision"],
                "approved_review_decision": row["proposed_review_decision"],
                "approval_bucket": row["decision_bucket"],
                "evidence_lane": row["evidence_lane"],
                "evidence_ref": row["evidence_ref"],
                "decision_evidence_ref": row["evidence_ref"],
                "approval_evidence_ref": (
                    ARTIFACT_PREFIX
                    + f"candidate-queue-review-decision-approval-{row['dedupe_key']}.json"
                ),
                "approval_notes": "human approval confirms decision row is ready for next gate",
                "hard_veto_present": row["hard_veto_present"],
                "stronger_existing_match_conflict": row[
                    "stronger_existing_match_conflict"
                ],
                "false_positive_guard_passed": row["false_positive_guard_passed"],
                "variant_conflict_checked": row["variant_conflict_checked"],
                "overwrite_protection_checked": row[
                    "overwrite_protection_checked"
                ],
                "exact_identity_confirmed": row["exact_identity_confirmed"],
            }
        )

    operator_approval = {
        "review_decision_approval_id": (
            "market-intel-candidate-writer-review-decision-approval-sample"
        ),
        "approval_id": "market-intel-candidate-writer-review-decision-approval-sample",
        "review_decision_id": decision["review_decision_id"],
        "review_inventory_id": decision["review_inventory_id"],
        "handoff_id": decision["handoff_id"],
        "inventory_review_id": decision["inventory_review_id"],
        "closeout_id": decision["closeout_id"],
        "run_package_id": decision["run_package_id"],
        "receipt_id": decision["receipt_id"],
        "target_table": TARGET_TABLE,
        "approval_scope": "candidate_queue_review_decision_approval",
        "approval_mode": "human_approval_only",
        "expected_decision_scope": "candidate_queue_review_decision",
        "expected_current_review_state": "needs_review",
        "approval_row_count": len(approval_rows),
        "approved_row_count": len(approval_rows),
        "expected_approval_row_count": decision["decision_row_count"],
        "expected_approved_row_count": decision["decision_row_count"],
        "expected_dedupe_keys": decision["expected_dedupe_keys"],
        "review_queue_artifact_path": decision["review_queue_artifact_path"],
        "review_inventory_artifact_path": decision[
            "review_inventory_artifact_path"
        ],
        "review_decision_artifact_path": decision["review_decision_artifact_path"],
        "approval_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-decision-approval-sample.json"
        ),
        "review_decision_approval_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-decision-approval-sample.json"
        ),
        "approval_rows": approval_rows,
        "approved_rows": approval_rows,
        "operator_confirmations": {
            "review_decision_checked": True,
            "approval_is_human_only": True,
            "human_approval_only": True,
            "manual_record_required": True,
            "approval_record_manual_only": True,
            "no_approval_token_payload": True,
            "no_api_cli_execution": True,
            "no_api_database_write": True,
            "no_api_review_state_update": True,
            "no_api_decision_record_write": True,
            "no_api_match_write": 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,
            "decision_rows_match_review_decision": True,
        },
        "api_execution_allowed": False,
        "real_write_allowed_by_api": False,
        "api_candidate_review_allowed": False,
        "api_updates_review_state": False,
        "api_writes_approval_record": False,
        "api_writes_decision_record": False,
        "api_writes_match_result": False,
    }
    _SAMPLE_WRITER_REVIEW_DECISION_APPROVAL_PACKAGE = {
        "writer_review_decision_package": decision_package,
        "writer_review_decision_result": decision_result,
        "operator_review_approval": operator_approval,
        "operator_review_decision_approval": operator_approval,
    }
    return deepcopy(_SAMPLE_WRITER_REVIEW_DECISION_APPROVAL_PACKAGE)
