"""
regime_strategies.py

[SIMULATION-ONLY 2026-09-04] Thin bridge between regime_detector.py's
regime labels and entry_engine.evaluate_entry()'s cfg_override param (see
that function's docstring). Deliberately does NOT reimplement any entry
check -- every real rule still lives in entry_engine.py; this module only
looks up which config.json regime_strategies[<regime>]["overrides"] dict
to shallow-merge on top of the base entry config for a given regime, so
"independent rules per regime" means independent CONFIG, not independent
(and divergent) code paths.

Not imported by monitor.py / entry_engine.py's live path. Currently used
by simulate_regime_strategies.py only.
"""

from config_loader import get_config
from entry_engine import evaluate_entry, EntryDecision


def get_regime_overrides(regime: str, force: bool = False) -> dict:
    """Returns the overrides dict for `regime`, or {} if regime_strategies
    is disabled, the regime has no entry yet, or the config path is
    missing -- callers always get a usable (possibly empty) dict rather
    than needing to guard against None.

    force=True bypasses the regime_strategies.enabled gate -- used by
    simulate_regime_strategies.py, which needs to read these rules for
    backtesting precisely BECAUSE enabled is still false (nothing live
    consults them yet). Live/future callers should leave force=False so
    the config's enabled flag stays the single on/off switch.
    """
    cfg = get_config().get("regime_strategies", {})
    if not force and not cfg.get("enabled", False):
        return {}
    return cfg.get(regime, {}).get("overrides", {})


def has_rules_for(regime: str) -> bool:
    cfg = get_config().get("regime_strategies", {})
    return regime in cfg and "overrides" in cfg.get(regime, {})


def evaluate_entry_for_regime(regime: str, symbol: str, premarket_result: dict,
                               live_bars: list, quote: tuple,
                               opening_baseline_volume: float,
                               health_reading=None, is_reentry: bool = False) -> EntryDecision:
    """
    Same signature/behavior as entry_engine.evaluate_entry(), except the
    regime's config.json overrides (if regime_strategies.enabled and this
    regime has an entry) are applied on top of the base entry config
    before any check runs. A regime with no overrides defined behaves
    identically to calling evaluate_entry() directly.
    """
    overrides = get_regime_overrides(regime)
    return evaluate_entry(
        symbol, premarket_result, live_bars, quote, opening_baseline_volume,
        health_reading=health_reading, is_reentry=is_reentry,
        cfg_override=overrides,
    )
