#!/usr/bin/env python3
from __future__ import annotations

import argparse
import json
import urllib.request
from pathlib import Path
from typing import Any
from urllib.error import HTTPError, URLError


DEFAULT_BASE_URL = "http://docker.cin.su:8011"
DEFAULT_MCP_URL = "http://docker.cin.su:8021"
EXPECTED_CONTRACT_VERSION = "onec-selector-contract.v1"


def post_json(url: str, payload: dict[str, Any], *, timeout: float, headers: dict[str, str] | None = None) -> tuple[dict[str, str], dict[str, Any]]:
    request = urllib.request.Request(
        url,
        data=json.dumps(payload, ensure_ascii=False).encode("utf-8"),
        method="POST",
        headers={"Content-Type": "application/json", **(headers or {})},
    )
    with urllib.request.urlopen(request, timeout=timeout) as response:
        raw = response.read().decode("utf-8")
        data = json.loads(raw) if raw else {}
        return dict(response.headers), data if isinstance(data, dict) else {"status": "error", "error": "response_not_object", "response": data}


def rest_rpc(base_url: str, method: str, payload: dict[str, Any], timeout: float) -> dict[str, Any]:
    _, data = post_json(
        base_url.rstrip("/") + "/rpc",
        {"method": method, "payload": payload},
        timeout=timeout,
        headers={"Content-Type": "application/json; charset=utf-8"},
    )
    return data


def mcp_initialize(mcp_url: str, timeout: float) -> tuple[str | None, dict[str, Any]]:
    body = {
        "jsonrpc": "2.0",
        "id": "write-rollback-safety-initialize",
        "method": "initialize",
        "params": {
            "protocolVersion": "2025-06-18",
            "capabilities": {},
            "clientInfo": {"name": "smoke_1c_write_rollback_safety", "version": "1"},
        },
    }
    try:
        headers, data = post_json(
            mcp_url.rstrip("/") + "/mcp",
            body,
            timeout=timeout,
            headers={"Accept": "application/json, text/event-stream"},
        )
    except HTTPError as exc:
        return None, {"status": "error", "error": "mcp_initialize_http_error", "http_status": exc.code, "response": exc.read().decode("utf-8", errors="replace")}
    except URLError as exc:
        return None, {"status": "error", "error": "mcp_initialize_url_error", "message": str(exc)}
    if "error" in data:
        return None, {"status": "error", "error": "mcp_initialize_jsonrpc_error", "details": data.get("error")}
    return headers.get("Mcp-Session-Id"), data


def mcp_rpc(mcp_url: str, method: str, payload: dict[str, Any], timeout: float, session_id: str | None) -> dict[str, Any]:
    headers = {"Accept": "application/json, text/event-stream"}
    if session_id:
        headers["Mcp-Session-Id"] = session_id
    body = {
        "jsonrpc": "2.0",
        "id": f"write-rollback-safety-{method}",
        "method": "tools/call",
        "params": {"name": "onec_request", "arguments": {"method": method, "payload": payload}},
    }
    try:
        _, data = post_json(mcp_url.rstrip("/") + "/mcp", body, timeout=timeout, headers=headers)
    except HTTPError as exc:
        return {"status": "error", "error": "mcp_http_error", "http_status": exc.code, "response": exc.read().decode("utf-8", errors="replace")}
    except URLError as exc:
        return {"status": "error", "error": "mcp_url_error", "message": str(exc)}
    if "error" in data:
        return {"status": "error", "error": "mcp_jsonrpc_error", "details": data.get("error")}
    result = data.get("result") if isinstance(data.get("result"), dict) else {}
    content = result.get("content") if isinstance(result.get("content"), list) else []
    first = content[0] if content and isinstance(content[0], dict) else {}
    text = first.get("text")
    if not isinstance(text, str):
        return {"status": "error", "error": "mcp_tool_text_missing", "result": result}
    try:
        decoded = json.loads(text)
    except json.JSONDecodeError as exc:
        return {"status": "error", "error": "mcp_tool_text_not_json", "message": str(exc), "text": text[:500]}
    return decoded if isinstance(decoded, dict) else {"status": "error", "error": "mcp_tool_payload_not_object", "payload": decoded}


def rpc_call(endpoint_url: str, method: str, payload: dict[str, Any], timeout: float, *, transport: str, session_id: str | None) -> dict[str, Any]:
    if transport == "mcp":
        return mcp_rpc(endpoint_url, method, payload, timeout, session_id)
    return rest_rpc(endpoint_url, method, payload, timeout)


def require(condition: bool, message: str, failures: list[str]) -> None:
    if not condition:
        failures.append(message)


def method_names(help_result: dict[str, Any]) -> set[str]:
    names: set[str] = set()
    for item in help_result.get("methods") or []:
        if isinstance(item, dict) and isinstance(item.get("name"), str):
            names.add(item["name"])
        elif isinstance(item, str):
            names.add(item)
    return names


def run_smoke(endpoint_url: str, base_id: str, timeout: float, *, transport: str) -> dict[str, Any]:
    failures: list[str] = []
    checks: dict[str, Any] = {}
    session_id: str | None = None
    initialize_result: dict[str, Any] | None = None
    if transport == "mcp":
        session_id, initialize_result = mcp_initialize(endpoint_url, timeout)
        contract_version = (((initialize_result.get("result") or {}).get("serverInfo") or {}).get("contract_version") if isinstance(initialize_result.get("result"), dict) else None)
        checks["mcp.initialize"] = {
            "status": "ok" if session_id and contract_version == EXPECTED_CONTRACT_VERSION else "error",
            "session": bool(session_id),
            "contract_version": contract_version,
        }
        require(bool(session_id), "MCP initialize must return Mcp-Session-Id", failures)
        require(contract_version == EXPECTED_CONTRACT_VERSION, "MCP initialize must report expected contract version", failures)

    help_result = rpc_call(endpoint_url, "help.methods", {}, timeout, transport=transport, session_id=session_id)
    names = method_names(help_result)
    checks["method_exposed"] = {
        "status": "ok" if "metadata.write.rollback" in names else "missing",
        "method_count": len(names),
        "method": "metadata.write.rollback",
    }
    require("metadata.write.rollback" in names, "metadata.write.rollback must be exposed by help.methods", failures)

    history = rpc_call(
        endpoint_url,
        "metadata.write.history",
        {"base_id": base_id, "limit": 1, "timeout_seconds": int(timeout)},
        timeout,
        transport=transport,
        session_id=session_id,
    )
    checks["history_available"] = {
        "schema": history.get("schema"),
        "status": history.get("status"),
        "operations": len(history.get("operations") or []),
    }
    require(history.get("status") == "ok", "metadata.write.history must be readable for rollback evidence", failures)

    blocked = rpc_call(
        endpoint_url,
        "metadata.write.rollback",
        {
            "base_id": base_id,
            "operation_id": "00000000000000000000000000000000",
            "timeout_seconds": int(timeout),
        },
        timeout,
        transport=transport,
        session_id=session_id,
    )
    checks["rollback_without_gate_blocked"] = {
        "schema": blocked.get("schema"),
        "status": blocked.get("status"),
        "error": blocked.get("error"),
        "argument": blocked.get("argument"),
        "applied": blocked.get("applied"),
        "has_rollback_result": isinstance(blocked.get("rollback_result"), dict),
    }
    require(blocked.get("status") == "invalid_argument", "rollback without explicit gate must be rejected", failures)
    require(blocked.get("argument") == "allow_sql_saved_state_rollback", "rollback rejection must point to allow_sql_saved_state_rollback", failures)
    require(blocked.get("applied") is not True, "rollback without explicit gate must not be applied", failures)
    require(not isinstance(blocked.get("rollback_result"), dict), "blocked rollback must not include a rollback_result", failures)

    return {
        "schema": "onec_write_rollback_safety_smoke.v1",
        "status": "ok" if not failures else "failed",
        "endpoint_url": endpoint_url,
        "transport": transport,
        "base_id": base_id,
        "checks": checks,
        "failures": failures,
        **({"mcp_initialize": initialize_result} if initialize_result and failures else {}),
    }


def main() -> int:
    parser = argparse.ArgumentParser(description="Smoke deployed metadata.write.rollback safety behavior without applying rollback.")
    parser.add_argument("--base-url", default=DEFAULT_BASE_URL)
    parser.add_argument("--mcp-url", default=DEFAULT_MCP_URL)
    parser.add_argument("--transport", choices=("rest", "mcp"), default="rest")
    parser.add_argument("--base-id", default="upo_test")
    parser.add_argument("--timeout", type=float, default=30.0)
    parser.add_argument("--report", type=Path)
    args = parser.parse_args()

    endpoint_url = args.mcp_url if args.transport == "mcp" else args.base_url
    report = run_smoke(endpoint_url, args.base_id, args.timeout, transport=args.transport)
    if args.report:
        args.report.parent.mkdir(parents=True, exist_ok=True)
        args.report.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    print(json.dumps(report, ensure_ascii=False, indent=2))
    return 0 if report["status"] == "ok" else 1


if __name__ == "__main__":
    raise SystemExit(main())
