from __future__ import annotations

import json
import sys
from pathlib import Path

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

from src.quant_engine.sector_trend_analysis import build_sector_trend_analysis

DEFAULT_JSON = ROOT / "GatherTradingData.json"
DEFAULT_OUT = ROOT / "Temp" / "sector_trend_analysis_v1.json"


def main() -> int:
    payload = {}
    if DEFAULT_JSON.exists():
        try:
            payload = json.loads(DEFAULT_JSON.read_text(encoding="utf-8"))
        except Exception:
            payload = {}
    result = build_sector_trend_analysis(payload if isinstance(payload, dict) else {})
    DEFAULT_OUT.parent.mkdir(parents=True, exist_ok=True)
    DEFAULT_OUT.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    print("SECTOR_TREND_ANALYSIS_V1")
    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0


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