"""Sample payload for the review decision approval writer preflight 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_approval import (
    build_mcp_fetch_candidate_queue_writer_review_decision_approval_preview,
)
from services.market_intel.mcp_fetch_candidate_queue_writer_review_decision_approval_writer_preflight_gates import (
    TARGET_OPERATION,
)


_SAMPLE_APPROVAL_WRITER_PREFLIGHT_PACKAGE = None


def build_sample_writer_review_decision_approval_writer_preflight_package():
    global _SAMPLE_APPROVAL_WRITER_PREFLIGHT_PACKAGE
    if _SAMPLE_APPROVAL_WRITER_PREFLIGHT_PACKAGE is not None:
        return deepcopy(_SAMPLE_APPROVAL_WRITER_PREFLIGHT_PACKAGE)

    approval_preview = (
        build_mcp_fetch_candidate_queue_writer_review_decision_approval_preview()
    )
    approval_package = approval_preview["sample_writer_review_decision_approval_package"]
    approval_result = (
        build_mcp_fetch_candidate_queue_writer_review_decision_approval_preview(
            writer_review_decision_package=approval_package[
                "writer_review_decision_package"
            ],
            writer_review_decision_result=approval_package[
                "writer_review_decision_result"
            ],
            operator_review_decision_approval=approval_package[
                "operator_review_decision_approval"
            ],
        )
    )
    approval = approval_result["operator_review_decision_approval_summary"]
    preflight_rows = []
    for row in approval["approved_rows"]:
        preflight_rows.append(
            {
                "dedupe_key": row["dedupe_key"],
                "current_review_state": row["current_review_state"],
                "approved_review_decision": row["approved_review_decision"],
                "target_review_state": row["approved_review_decision"],
                "statement_type": "update_review_state",
                "operation": "update",
                "writer_action": "prepare_cli_review_state_update",
                "evidence_lane": row["evidence_lane"],
                "decision_evidence_ref": row["decision_evidence_ref"],
                "approval_evidence_ref": row["approval_evidence_ref"],
                "preflight_evidence_ref": (
                    ARTIFACT_PREFIX
                    + "candidate-queue-review-decision-approval-writer-preflight-"
                    + f"{row['dedupe_key']}.json"
                ),
                "preflight_notes": (
                    "approved review decision mapped to CLI-only writer preflight"
                ),
                "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_writer_preflight = {
        "writer_preflight_id": (
            "market-intel-candidate-writer-review-decision-approval-preflight-sample"
        ),
        "approval_id": approval["approval_id"],
        "review_decision_id": approval["review_decision_id"],
        "review_inventory_id": approval["review_inventory_id"],
        "handoff_id": approval["handoff_id"],
        "inventory_review_id": approval["inventory_review_id"],
        "closeout_id": approval["closeout_id"],
        "run_package_id": approval["run_package_id"],
        "receipt_id": approval["receipt_id"],
        "target_table": TARGET_TABLE,
        "target_operation": TARGET_OPERATION,
        "preflight_scope": (
            "candidate_queue_review_decision_approval_writer_preflight"
        ),
        "preflight_mode": "cli_only_writer_preflight_preview",
        "expected_current_review_state": "needs_review",
        "preflight_row_count": len(preflight_rows),
        "expected_preflight_row_count": approval["approved_row_count"],
        "expected_dedupe_keys": approval["expected_dedupe_keys"],
        "review_queue_artifact_path": approval["review_queue_artifact_path"],
        "review_inventory_artifact_path": approval[
            "review_inventory_artifact_path"
        ],
        "review_decision_artifact_path": approval["review_decision_artifact_path"],
        "approval_artifact_path": approval["approval_artifact_path"],
        "writer_preflight_artifact_path": (
            ARTIFACT_PREFIX
            + "candidate-queue-review-decision-approval-writer-preflight-sample.json"
        ),
        "preflight_rows": preflight_rows,
        "operator_confirmations": {
            "approval_checked": True,
            "writer_preflight_is_cli_only": True,
            "manual_record_required": 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,
            "rows_match_approval": True,
            "schema_preflight_required": True,
            "status_transition_reviewed": 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_preflight_record": False,
        "api_writes_approval_record": False,
        "api_writes_decision_record": False,
        "api_writes_match_result": False,
    }
    _SAMPLE_APPROVAL_WRITER_PREFLIGHT_PACKAGE = {
        "writer_review_decision_approval_package": approval_package,
        "writer_review_decision_approval_result": approval_result,
        "operator_writer_preflight": operator_writer_preflight,
        "operator_review_decision_approval_writer_preflight": (
            operator_writer_preflight
        ),
    }
    return deepcopy(_SAMPLE_APPROVAL_WRITER_PREFLIGHT_PACKAGE)
