"""
选题收集模块
调用 scripts/collector.py 脚本，从外部来源收集/生成新选题并写入 JSON
"""
import subprocess
from pathlib import Path
import logging
import os

logger = logging.getLogger(__name__)

# 计算项目根目录
PROJECT_ROOT = Path(__file__).resolve().parents[4]
if os.getenv('PROJECT_ROOT'):
    PROJECT_ROOT = Path(os.getenv('PROJECT_ROOT'))

def run_collector():
    """运行选题收集脚本"""
    script_path = PROJECT_ROOT / "scripts" / "collector.py"
    if not script_path.exists():
        raise FileNotFoundError(f"Collector script not found: {script_path}")
    result = subprocess.run(
        ["python", str(script_path)],
        capture_output=True,
        text=True,
        cwd=PROJECT_ROOT,
        timeout=300
    )
    if result.returncode != 0:
        raise RuntimeError(f"Collector failed: {result.stderr}")
    return {"ok": True, "output": result.stdout}