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

import json
import sys
from pathlib import Path
import yaml

ROOT = Path(__file__).resolve().parent.parent

def main() -> int:
    todo_path = ROOT / "spec" / "23_low_capability_llm_pipeline_todo.yaml"
    if not todo_path.exists():
        print(f"Todo spec missing at {todo_path}")
        return 1
        
    try:
        data = yaml.safe_load(todo_path.read_text(encoding="utf-8")) or {}
        todo_data = data.get("low_capability_llm_pipeline_todo", {})
        ordered_steps = todo_data.get("ordered_steps", [])
    except Exception as e:
        print(f"Failed to parse todo YAML: {e}")
        return 1
        
    step_count = len(ordered_steps)
    ambiguous_count = 0
    calculation_count = 0
    
    # Simple keyword analysis for safety
    ambiguous_keywords = {"대략", "대체로", "임의", "적당히", "approximate", "guess", "assume"}
    calculation_keywords = {"계산", "더하", "빼", "곱하", "나누", "평균", "합계", "calculate", "math", "add", "multiply", "divide", "average", "sum"}
    
    for step in ordered_steps:
        action = str(step.get("action", "")).lower()
        is_negative = any(neg in action for neg in {"제거", "배제", "금지", "없이"})
        if step.get("ambiguous", False) or (any(k in action for k in ambiguous_keywords) and not is_negative):
            ambiguous_count += 1
        if step.get("calculation", False) or (any(k in action for k in calculation_keywords) and not is_negative):
            calculation_count += 1
            
    gate_passed = (step_count >= 12) and (ambiguous_count == 0) and (calculation_count == 0)
    
    result = {
        "formula_id": "LOW_CAPABILITY_PIPELINE_TODO_VALIDATOR_V2",
        "low_capability_step_count": step_count,
        "ambiguous_instruction_count": ambiguous_count,
        "calculation_instruction_count": calculation_count,
        "gate": "PASS" if gate_passed else "FAIL"
    }
    
    # Save validation packet to Temp
    out_dir = ROOT / "Temp"
    out_dir.mkdir(parents=True, exist_ok=True)
    out_path = out_dir / "low_capability_pipeline_todo_validation_v2.json"
    out_path.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    
    print(json.dumps(result, ensure_ascii=True, indent=2))
    return 0 if gate_passed else 1

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