"""WBS-7.3 parity 테스트 — GAS 원본을 Node로 직접 실행해 Python 포팅과 대조한다.

GAS 함수를 손으로 다시 옮겨 적은 뒤 "맞겠지"라고 가정하지 않는다 — 매 테스트
실행마다 src/gas_adapter_parts/gdf_03_portfolio_gates.gs에서 classifyOrderType_
함수 소스를 그대로 추출해 Node로 실행하고, formulas/stop_loss_gate_v1.py의
Python 포트와 동일 입력에 대해 동일 출력을 내는지 확인한다. GAS 원본이
나중에 바뀌면 이 테스트가 즉시 drift를 잡아낸다(수작업 동기화에 의존하지 않음).
"""
from __future__ import annotations

import json
import shutil
import subprocess
import sys
from pathlib import Path

import pytest

ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from formulas.stop_loss_gate_v1 import classify_order_type

GAS_SOURCE = ROOT / "src" / "gas_adapter_parts" / "gdf_03_portfolio_gates.gs"
FUNCTION_NAME = "classifyOrderType_"

TEST_CASES: list[tuple[str, dict | None]] = [
    ("BUY_A", {"stopBreach": False}),
    ("BUY_PILOT", None),
    ("ANYTHING", {"stopBreach": True}),
    ("EXIT_FULL", {"stopBreach": False}),
    ("SELL_TRIM_25", None),
    ("TRIM_33", {"stopBreach": False}),
    ("ROTATE_OUT", None),
    ("HOLD", None),
    ("HOLD", {"stopBreach": False}),
    ("WATCH_ONLY", None),
    ("", None),
    ("BUY_PILOT", {"stopBreach": True}),  # stopBreach가 BUY 신호보다 우선해야 함
]


def _extract_gas_function(source_text: str, function_name: str) -> str:
    marker = f"function {function_name}("
    start = source_text.index(marker)
    brace_start = source_text.index("{", start)
    depth = 0
    for i in range(brace_start, len(source_text)):
        if source_text[i] == "{":
            depth += 1
        elif source_text[i] == "}":
            depth -= 1
            if depth == 0:
                return source_text[start : i + 1]
    raise ValueError(f"unbalanced braces while extracting {function_name}")


@pytest.fixture(scope="module")
def gas_function_source() -> str:
    text = GAS_SOURCE.read_text(encoding="utf-8")
    return _extract_gas_function(text, FUNCTION_NAME)


@pytest.fixture(scope="module")
def node_available() -> bool:
    return shutil.which("node") is not None


def _run_via_node(function_source: str, cases: list[tuple[str, dict | None]]) -> list[str]:
    driver = f"""
{function_source}
const cases = {json.dumps(cases)};
const results = cases.map(([signalCode, holding]) => {FUNCTION_NAME}(signalCode, holding));
console.log(JSON.stringify(results));
"""
    proc = subprocess.run(["node", "-e", driver], capture_output=True, text=True, timeout=20)
    if proc.returncode != 0:
        raise RuntimeError(f"node execution failed: {proc.stderr}")
    return json.loads(proc.stdout)


def test_gas_function_still_extractable(gas_function_source: str):
    """추출 자체가 실패하면(함수명 변경/삭제) 즉시 드러나야 한다."""
    assert "function classifyOrderType_" in gas_function_source
    assert "STOP_LOSS" in gas_function_source


def test_python_port_matches_live_gas_source(gas_function_source: str, node_available: bool):
    if not node_available:
        pytest.skip("node not available in this environment")

    gas_results = _run_via_node(gas_function_source, TEST_CASES)
    python_results = [classify_order_type(signal_code, holding) for signal_code, holding in TEST_CASES]

    mismatches = [
        (TEST_CASES[i], gas_results[i], python_results[i])
        for i in range(len(TEST_CASES))
        if gas_results[i] != python_results[i]
    ]
    assert not mismatches, f"GAS-Python parity 불일치: {mismatches}"
