#!/usr/bin/env python3
"""P4_01: 라우팅·서빙·판단 단일화 — SCALP/SWING/MOMENTUM/POSITION 결정론화"""

from __future__ import annotations
import json, sys
from pathlib import Path
from datetime import datetime

ROOT = Path(__file__).resolve().parent.parent
OUTPUT = ROOT / "Temp" / "p4_01_routing_packet_spec.json"

if sys.stdout.encoding and sys.stdout.encoding.lower() not in ("utf-8", "utf8"):
    sys.stdout = open(sys.stdout.fileno(), mode="w", encoding="utf-8", buffering=1)

def build_routing_spec() -> dict:
    return {
        "schema_version": "unified_route_packet_v1",
        "generated_at": datetime.now().isoformat(),
        "purpose": "SCALP/SWING/MOMENTUM/POSITION 판단을 결정론적 JSON으로 잠금",
        "route_dimensions": ["SCALP", "SWING", "MOMENTUM", "POSITION"],
        "style_weights": {
            "SCALP": {"technical": 0.50, "smart_money": 0.25, "liquidity": 0.15, "fundamental": 0.10},
            "SWING": {"smart_money": 0.35, "technical": 0.30, "liquidity": 0.20, "fundamental": 0.15},
            "MOMENTUM": {"fundamental": 0.40, "smart_money": 0.30, "technical": 0.20, "liquidity": 0.10},
            "POSITION": {"fundamental": 0.55, "smart_money": 0.20, "liquidity": 0.15, "technical": 0.10}
        },
        "conviction_to_pct": {
            "<35": "진입 금지",
            "35-49": "1.5% (PILOT)",
            "50-64": "3%",
            "65-79": "5%",
            "80+": "7%"
        },
        "route_formula": "score = weighted_score × data_quality × regime_scale × anti_chase × liquidity × cash",
        "mandatory_output": [
            "ticker별 4스타일 점수(0-100)",
            "best_style",
            "recommended_pct",
            "blocked_reason_codes (if blocked)"
        ],
        "implementation_files": [
            "spec/xx_routing_contract.yaml",
            "gas_data_feed.gs: buildRoutePacket_()",
            "tools/validate_capital_style_allocation_v1.py"
        ]
    }

def main() -> int:
    print("=" * 70)
    print("  P4_01: 라우팅·서빙·판단 단일화")
    print("=" * 70)
    spec = build_routing_spec()
    OUTPUT.write_text(json.dumps(spec, ensure_ascii=False, indent=2), encoding="utf-8")
    print(f"\n✓ 라우팅 스펙 저장: {OUTPUT.name}")
    print(f"  4가지 스타일 권중 정의 완료")
    print(f"  LLM 자유도 제거: 결정론적 JSON으로 잠금")
    return 0

if __name__ == "__main__":
    sys.exit(main())
