import json
import subprocess
import sys
from datetime import datetime, timezone


def _write_source_contract_replay_receipt(
    root,
    *,
    platform="coupang_tw",
    manifest_id="coupang-403",
    adapter_code="coupang",
    barrier_type="access_denied",
    capture_runtime_unavailable=False,
):
    receipt_dir = root / platform / manifest_id
    receipt_dir.mkdir(parents=True)
    payload = {
        "generated_at": datetime.now(timezone.utc).isoformat(),
        "policy": "controlled_pixelrag_source_contract_replay_worker_v1",
        "worker_status": "executed_source_contract_replay_ready",
        "platform": platform,
        "manifest_id": manifest_id,
        "adapter_code": adapter_code,
        "source_worker_receipt_path": (
            f"runtime_artifacts/pixelrag_platform_probe_worker_receipts/"
            f"{platform}/{manifest_id}/platform_probe_worker_receipt.json"
        ),
        "source_receipt_path": (
            f"runtime_artifacts/pixelrag_visual_evidence/"
            f"{platform}/{manifest_id}/capture_receipt.json"
        ),
        "source_contract_check_count": 8,
        "source_contract_check_pass_count": 8,
        "source_contract_checks": {
            "blocked_page_not_product_data": True,
            "database_write_disallowed": True,
            "dry_run_only_contract": True,
            "network_request_deferred": True,
            "source_count_positive": True,
            "source_receipt_path_present": True,
            "source_worker_receipt_path_present": True,
            "structured_source_package_available": True,
        },
        "source_contract_preview": {
            "source_contract_version": "pixelrag_marketplace_source_contract_v1",
            "contract_id": f"{platform}:{manifest_id}:{adapter_code}",
            "adapter_code": adapter_code,
            "barrier_type": barrier_type,
            "capture_runtime_unavailable": capture_runtime_unavailable,
            "source_count": 2,
            "expected_offer_fields": [
                "platform",
                "platform_product_id",
                "title",
                "url",
                "image_url",
                "price",
                "currency",
                "availability",
                "seller",
                "promotion_badges",
                "shipping_badges",
                "evidence_time",
                "source_method",
                "confidence",
            ],
            "required_before_data_promotion": [
                "public_source_boundary",
                "rate_limit_contract",
                "provenance_contract",
                "identity_matcher_replay",
                "promotion_gate",
                "embedding_signature_guard",
            ],
        },
        "promotion_boundary": {
            "direct_ai_insights_write_allowed": False,
            "direct_price_write_allowed": False,
        },
        "writes_database": False,
        "network_call_performed": False,
        "model_call_performed": False,
    }
    path = receipt_dir / "source_contract_replay_receipt.json"
    path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
    return path


def test_marketplace_source_contract_adapter_preflight_dry_run_reads_receipts(tmp_path):
    from services.pixelrag_marketplace_source_contract_adapter_preflight_service import (
        POLICY,
        run_pixelrag_marketplace_source_contract_adapter_preflight,
    )

    source_root = tmp_path / "source_contracts"
    _write_source_contract_replay_receipt(source_root)
    _write_source_contract_replay_receipt(
        source_root,
        platform="shopee_tw",
        manifest_id="shopee-runtime-gap",
        adapter_code="shopee",
        barrier_type="language_or_region_interstitial",
        capture_runtime_unavailable=True,
    )

    payload = run_pixelrag_marketplace_source_contract_adapter_preflight(
        source_contract_replay_receipt_root=source_root,
        platform=("coupang_tw", "shopee_tw"),
    )
    by_platform = {item["platform"]: item for item in payload["worker_items"]}

    assert payload["policy"] == POLICY
    assert payload["status"] == "ok"
    assert payload["summary"]["candidate_count"] == 2
    assert payload["summary"]["ready_count"] == 2
    assert payload["summary"]["dry_run_count"] == 2
    assert payload["summary"]["capture_runtime_gap_count"] == 1
    assert payload["summary"]["writes_database_count"] == 0
    assert payload["controlled_apply"]["network_call"] is False
    assert payload["controlled_apply"]["writes_database"] is False
    assert payload["next_machine_action"] == (
        "run_marketplace_source_contract_adapter_preflight_execute"
    )
    assert by_platform["coupang_tw"]["worker_status"] == (
        "dry_run_ready_for_adapter_preflight"
    )
    assert by_platform["coupang_tw"]["adapter_preflight_checks"][
        "rate_limit_contract_required"
    ] is True
    assert by_platform["shopee_tw"]["adapter_preflight"][
        "capture_runtime_unavailable"
    ] is True


def test_marketplace_source_contract_adapter_preflight_execute_writes_receipt(tmp_path):
    from services.pixelrag_marketplace_source_contract_adapter_preflight_service import (
        run_pixelrag_marketplace_source_contract_adapter_preflight,
    )

    source_root = tmp_path / "source_contracts"
    output_root = tmp_path / "adapter_preflight"
    _write_source_contract_replay_receipt(source_root)

    payload = run_pixelrag_marketplace_source_contract_adapter_preflight(
        source_contract_replay_receipt_root=source_root,
        output_root=output_root,
        platform="coupang_tw",
        execute=True,
        write_receipt=True,
    )

    assert payload["status"] == "ok"
    assert payload["summary"]["executed_count"] == 1
    assert payload["summary"]["receipt_written_count"] == 1
    assert payload["controlled_apply"]["artifact_write"] is True
    assert payload["controlled_apply"]["writes_database"] is False
    assert payload["next_machine_action"] == "run_marketplace_source_contract_adapter_dry_run"
    receipt_path = (
        output_root
        / "coupang_tw"
        / "coupang-403"
        / "marketplace_adapter_preflight_receipt.json"
    )
    assert receipt_path.exists()
    receipt = json.loads(receipt_path.read_text(encoding="utf-8"))
    assert receipt["worker_status"] == "executed_adapter_preflight_ready"
    assert receipt["artifact_write_performed"] is True
    assert receipt["writes_database"] is False
    assert receipt["adapter_preflight"]["adapter_preflight_version"] == (
        "pixelrag_marketplace_adapter_preflight_v1"
    )


def test_marketplace_source_contract_adapter_preflight_cli_outputs_json(tmp_path):
    source_root = tmp_path / "source_contracts"
    _write_source_contract_replay_receipt(source_root)

    completed = subprocess.run(
        [
            sys.executable,
            "scripts/ops/run_pixelrag_marketplace_source_contract_adapter_preflight.py",
            "--source-contract-replay-receipt-root",
            str(source_root),
            "--platform",
            "coupang_tw",
        ],
        capture_output=True,
        check=False,
        text=True,
    )

    assert completed.returncode == 0
    payload = json.loads(completed.stdout)
    assert payload["summary"]["candidate_count"] == 1
    assert payload["summary"]["ready_count"] == 1
    assert payload["controlled_apply"]["writes_database"] is False


def test_marketplace_source_contract_adapter_preflight_route_returns_readback(
    tmp_path,
    monkeypatch,
):
    from flask import Flask
    from routes import system_public_routes as routes
    from services import pixelrag_marketplace_source_contract_adapter_preflight_service as service

    source_root = tmp_path / "source_contracts"
    _write_source_contract_replay_receipt(source_root)
    monkeypatch.setattr(
        service,
        "DEFAULT_SOURCE_CONTRACT_REPLAY_RECEIPT_ROOT",
        str(source_root),
    )

    app = Flask(__name__)
    with app.test_request_context(
        "/api/ai-automation/pixelrag-marketplace-source-contract-adapter-preflight?platform=coupang_tw"
    ):
        response = (
            routes
            .ai_automation_pixelrag_marketplace_source_contract_adapter_preflight_api
            .__wrapped__()
        )
    payload = response.get_json()

    assert payload["policy"] == (
        "controlled_pixelrag_marketplace_source_contract_adapter_preflight_v1"
    )
    assert payload["summary"]["candidate_count"] == 1
    assert payload["summary"]["ready_count"] == 1
    assert payload["controlled_apply"]["writes_database"] is False
