"""
Template strategy: reads portfolio + market context, emits intent JSON only.

Copy this module, rename the class, set ``strategy_id``, implement real
signals/orders, and register in ``live_platform_default.json``.
"""

from __future__ import annotations

import json
from pathlib import Path

from RenTech.live.protocols import StrategyContext, StrategyCycleReport


class ExampleSignalOnlyStrategy:
    strategy_id = "example_signal_only"

    async def audit(self, ctx: StrategyContext) -> StrategyCycleReport:
        return StrategyCycleReport(
            strategy_id=self.strategy_id,
            ok=True,
            messages=["example audit: no broker positions expected for this book"],
        )

    async def run_cycle(self, ctx: StrategyContext) -> StrategyCycleReport:
        spy_legs = ctx.portfolio.positions_by_symbol.get("SPY", ())
        opt_count = len(ctx.portfolio.open_option_legs())
        intent = {
            "strategy_id": self.strategy_id,
            "run_id": ctx.run_id,
            "decision": "NO_TRADE",
            "reason": "template — replace with your signal engine",
            "nav_usd": ctx.portfolio.net_liquidation_usd,
            "budget_usd": ctx.capital_budget_usd,
            "spy_stock_legs": len(spy_legs),
            "total_option_legs": opt_count,
            "allow_new_entries": ctx.strategy_risk.allow_new_entries,
        }
        out = ctx.log_dir / f"{self.strategy_id}_intent_{ctx.run_id}.json"
        out.parent.mkdir(parents=True, exist_ok=True)
        out.write_text(json.dumps(intent, indent=2) + "\n", encoding="utf-8")
        return StrategyCycleReport(
            strategy_id=self.strategy_id,
            ok=True,
            messages=[f"wrote intent {out}"],
            metadata=intent,
        )
