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


def _write_receipt(root, *, platform, manifest_id, title, url, http_status=200):
    receipt_dir = root / platform / manifest_id
    tiles_dir = receipt_dir / "tiles"
    tiles_dir.mkdir(parents=True)
    screenshot = receipt_dir / "screenshot.png"
    screenshot.write_bytes(b"png")
    files = [{"kind": "screenshot", "path": str(screenshot)}]
    for index in range(4):
        tile = tiles_dir / f"tile-{index}.png"
        tile.write_bytes(b"tile")
        files.append({"kind": "tile", "path": str(tile)})
    receipt = {
        "generated_at": datetime.now(timezone.utc).isoformat(),
        "status": "captured",
        "manifest_id": manifest_id,
        "http_status": http_status,
        "capture_target": {
            "platform": platform,
            "url": url,
            "crawler": "PixelRAGMarketplaceSearch.visual_fallback",
        },
        "page_metrics": {
            "title": title,
            "final_url": url,
        },
        "tile_plan": {
            "planned_tile_count": 4,
            "emitted_tile_count": 4,
        },
        "files": files,
    }
    path = receipt_dir / "capture_receipt.json"
    path.write_text(json.dumps(receipt, ensure_ascii=False), encoding="utf-8")
    return path


def _write_platform_probe_worker_receipt(
    root,
    *,
    platform,
    manifest_id,
    worker_status="executed_structured_source_fallback_package",
    barrier_type="access_denied",
    capture_runtime_unavailable=False,
):
    receipt_dir = root / platform / manifest_id
    receipt_dir.mkdir(parents=True)
    payload = {
        "platform": platform,
        "manifest_id": manifest_id,
        "source_type": "capture_receipt",
        "source_receipt_path": f"runtime_artifacts/pixelrag_visual_evidence/{platform}/{manifest_id}/capture_receipt.json",
        "worker_status": worker_status,
        "probe_status": "structured_source_or_backoff_required",
        "barrier_type": barrier_type,
        "network_call_performed": False,
        "artifact_write_performed": True,
        "writes_database": False,
        "next_machine_action": "run_structured_source_or_platform_backoff_policy",
        "structured_source_package": {
            "adapter_code": platform.replace("_tw", ""),
            "available": True,
            "source_count": 2,
            "network_request_allowed": False,
            "database_write_allowed": False,
            "dry_run_only": True,
            "blocked_page_not_product_data": True,
            "capture_runtime_unavailable": capture_runtime_unavailable,
        },
    }
    path = receipt_dir / "platform_probe_worker_receipt.json"
    path.write_text(json.dumps(payload, ensure_ascii=False), encoding="utf-8")
    return path


def test_pixelrag_rag_candidate_replay_splits_eligible_and_blocked_receipts(tmp_path):
    from services.pixelrag_rag_candidate_replay_service import (
        POLICY,
        build_pixelrag_rag_candidate_replay_readback,
    )

    _write_receipt(
        tmp_path,
        platform="shopee_tw",
        manifest_id="shopee-ok",
        title="Shopee 防曬乳",
        url="https://shopee.tw/search?keyword=%E9%98%B2%E6%9B%AC%E4%B9%B3",
    )
    _write_receipt(
        tmp_path,
        platform="coupang_tw",
        manifest_id="coupang-403",
        title="Access Denied",
        url="https://www.tw.coupang.com/search?q=iphone",
        http_status=403,
    )

    payload = build_pixelrag_rag_candidate_replay_readback(
        artifact_root=tmp_path,
        platform_probe_worker_receipt_root=tmp_path / "worker_empty",
    )
    by_platform = {item["platform"]: item for item in payload["candidates"]}

    assert payload["policy"] == POLICY
    assert payload["status"] == "warning"
    assert payload["summary"]["receipt_count"] == 2
    assert payload["summary"]["eligible_count"] == 1
    assert payload["summary"]["blocked_count"] == 1
    assert payload["summary"]["visual_barrier_count"] == 1
    assert by_platform["shopee_tw"]["eligible_for_rag_candidate_replay"] is True
    assert by_platform["shopee_tw"]["promotion_boundary"]["direct_ai_insights_write_allowed"] is False
    assert "Shopee 防曬乳" in by_platform["shopee_tw"]["rag_candidate_text"]
    assert by_platform["coupang_tw"]["eligible_for_rag_candidate_replay"] is False
    assert by_platform["coupang_tw"]["visual_barrier_reason"] == "http_status_403"
    assert by_platform["coupang_tw"]["next_machine_action"] == "run_platform_probe_or_use_structured_api"
    assert payload["replay_contract"]["blocked_pages_are_not_product_data"] is True
    assert payload["controlled_apply"]["db_write"] is False


def test_pixelrag_rag_candidate_replay_reads_platform_probe_worker_receipts(tmp_path):
    from services.pixelrag_rag_candidate_replay_service import (
        build_pixelrag_rag_candidate_replay_readback,
    )

    worker_root = tmp_path / "worker"
    _write_platform_probe_worker_receipt(
        worker_root,
        platform="coupang_tw",
        manifest_id="coupang-403",
        barrier_type="access_denied",
    )
    _write_platform_probe_worker_receipt(
        worker_root,
        platform="shopee_tw",
        manifest_id="shopee-runtime-gap",
        worker_status="capture_runtime_unavailable_structured_fallback_package",
        barrier_type="traffic_verification_interstitial",
        capture_runtime_unavailable=True,
    )

    payload = build_pixelrag_rag_candidate_replay_readback(
        artifact_root=tmp_path / "visual_empty",
        platform_probe_worker_receipt_root=worker_root,
        platform=("coupang_tw", "shopee_tw"),
    )
    by_platform = {item["platform"]: item for item in payload["candidates"]}

    assert payload["status"] == "ok"
    assert payload["summary"]["receipt_count"] == 2
    assert payload["summary"]["visual_receipt_count"] == 0
    assert payload["summary"]["platform_probe_worker_receipt_count"] == 2
    assert payload["summary"]["source_contract_fallback_count"] == 2
    assert payload["summary"]["capture_runtime_gap_count"] == 1
    assert payload["next_machine_action"] == (
        "run_source_contract_replay_for_platform_probe_worker_receipts"
    )
    assert by_platform["coupang_tw"]["candidate_status"] == "source_contract_fallback"
    assert by_platform["coupang_tw"]["eligible_for_source_contract_replay"] is True
    assert by_platform["coupang_tw"]["eligible_for_rag_candidate_replay"] is False
    assert by_platform["coupang_tw"]["promotion_boundary"][
        "requires_source_contract_before_knowledge_write"
    ] is True
    assert by_platform["shopee_tw"]["platform_probe_worker"][
        "structured_source_package"
    ]["capture_runtime_unavailable"] is True
    assert payload["replay_contract"][
        "platform_probe_worker_receipts_are_source_contract_candidates"
    ] is True
    assert payload["controlled_apply"]["db_write"] is False


def test_pixelrag_rag_candidate_replay_prefers_worker_source_contract_handoff(tmp_path):
    from services.pixelrag_rag_candidate_replay_service import (
        build_pixelrag_rag_candidate_replay_readback,
    )

    visual_root = tmp_path / "visual"
    worker_root = tmp_path / "worker"
    _write_receipt(
        visual_root,
        platform="coupang_tw",
        manifest_id="coupang-403",
        title="Access Denied",
        url="https://www.tw.coupang.com/search?q=iphone",
        http_status=403,
    )
    _write_platform_probe_worker_receipt(
        worker_root,
        platform="coupang_tw",
        manifest_id="coupang-403",
        barrier_type="access_denied",
    )

    payload = build_pixelrag_rag_candidate_replay_readback(
        artifact_root=visual_root,
        platform_probe_worker_receipt_root=worker_root,
        platform="coupang_tw",
    )

    assert payload["summary"]["visual_barrier_count"] == 1
    assert payload["summary"]["source_contract_fallback_count"] == 1
    assert payload["next_machine_action"] == (
        "run_source_contract_replay_for_platform_probe_worker_receipts"
    )


def test_pixelrag_rag_candidate_replay_combines_ready_and_source_contract_actions(tmp_path):
    from services.pixelrag_rag_candidate_replay_service import (
        build_pixelrag_rag_candidate_replay_readback,
    )

    visual_root = tmp_path / "visual"
    worker_root = tmp_path / "worker"
    _write_receipt(
        visual_root,
        platform="shopee_tw",
        manifest_id="shopee-ok",
        title="Shopee 防曬乳",
        url="https://shopee.tw/search?keyword=sunscreen",
    )
    _write_platform_probe_worker_receipt(
        worker_root,
        platform="coupang_tw",
        manifest_id="coupang-403",
        barrier_type="access_denied",
    )

    payload = build_pixelrag_rag_candidate_replay_readback(
        artifact_root=visual_root,
        platform_probe_worker_receipt_root=worker_root,
        platform=("shopee_tw", "coupang_tw"),
    )

    assert payload["summary"]["eligible_count"] == 1
    assert payload["summary"]["source_contract_fallback_count"] == 1
    assert payload["next_machine_action"] == (
        "run_ocr_vlm_replay_and_source_contract_replay"
    )


def test_pixelrag_rag_candidate_replay_cli_outputs_machine_readable_json(tmp_path):
    _write_receipt(
        tmp_path,
        platform="shopee_tw",
        manifest_id="shopee-ok",
        title="Shopee 防曬乳",
        url="https://shopee.tw/search?keyword=sunscreen",
    )

    completed = subprocess.run(
        [
            sys.executable,
            "scripts/ops/report_pixelrag_rag_candidate_replay.py",
            "--artifact-root",
            str(tmp_path),
            "--platform",
            "shopee_tw",
            "--platform-probe-worker-receipt-root",
            str(tmp_path / "worker_empty"),
        ],
        capture_output=True,
        check=False,
        text=True,
    )

    assert completed.returncode == 0
    payload = json.loads(completed.stdout)
    assert payload["summary"]["receipt_count"] == 1
    assert payload["summary"]["eligible_count"] == 1
    assert payload["candidates"][0]["platform"] == "shopee_tw"


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

    _write_receipt(
        tmp_path,
        platform="shopee_tw",
        manifest_id="shopee-ok",
        title="Shopee 防曬乳",
        url="https://shopee.tw/search?keyword=sunscreen",
    )
    monkeypatch.setattr(service, "DEFAULT_ARTIFACT_ROOT", str(tmp_path))
    monkeypatch.setattr(
        service,
        "DEFAULT_PLATFORM_PROBE_WORKER_RECEIPT_ROOT",
        str(tmp_path / "worker_empty"),
    )

    app = Flask(__name__)
    with app.test_request_context(
        "/api/ai-automation/pixelrag-rag-candidate-replay?platform=shopee_tw"
    ):
        response = routes.ai_automation_pixelrag_rag_candidate_replay_api.__wrapped__()
    payload = response.get_json()

    assert payload["policy"] == "read_only_pixelrag_rag_candidate_replay_v1"
    assert payload["summary"]["receipt_count"] == 1
    assert payload["summary"]["eligible_count"] == 1
    assert payload["replay_contract"]["writes_database"] is False
