# source: https://raw.githubusercontent.com/xegheplimo-web/freqtrade-strategy-miner/a17d95b8b8721b031d98b1d8ced17a96df2d646d/src/strategy_miner/compiler.py
# directory_url: https://github.com/xegheplimo-web/freqtrade-strategy-miner/blob/main/src/strategy_miner/
# User: xegheplimo-web
# Repository: freqtrade-strategy-miner
# --------------------from __future__ import annotations

import ast
from pathlib import Path

from .genome import StrategyGenome

STRATEGY_TEMPLATE = '''from freqtrade.strategy import IStrategy, IntParameter
from pandas import DataFrame
import talib.abstract as ta


class Github_xegheplimo_web_freqtrade_strategy_miner__compiler__20261008_043029(IStrategy):
    INTERFACE_VERSION = 3

    timeframe = {timeframe!r}
    can_short = {can_short}
    process_only_new_candles = True
    startup_candle_count = {startup_candle_count}

    minimal_roi = {{"0": 0.10, "60": 0.04, "180": 0.0}}
    stoploss = {stoploss}
    trailing_stop = False
    use_exit_signal = True

    buy_rsi_long_max = IntParameter(20, 45, default={rsi_long_max}, space="buy")
    buy_rsi_short_min = IntParameter(55, 80, default={rsi_short_min}, space="buy")

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe["ema_fast"] = ta.EMA(dataframe, timeperiod={ema_fast})
        dataframe["ema_slow"] = ta.EMA(dataframe, timeperiod={ema_slow})
        dataframe["rsi"] = ta.RSI(dataframe, timeperiod={rsi_period})
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        long_condition = (
            (dataframe["ema_fast"] > dataframe["ema_slow"]) &
            (dataframe["rsi"] < self.buy_rsi_long_max.value) &
            (dataframe["volume"] > 0)
        )
        dataframe.loc[long_condition, ["enter_long", "enter_tag"]] = (1, "trend_rsi_long")

        if self.can_short:
            short_condition = (
                (dataframe["ema_fast"] < dataframe["ema_slow"]) &
                (dataframe["rsi"] > self.buy_rsi_short_min.value) &
                (dataframe["volume"] > 0)
            )
            dataframe.loc[short_condition, ["enter_short", "enter_tag"]] = (1, "trend_rsi_short")

        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        long_exit = (
            (dataframe["ema_fast"] < dataframe["ema_slow"]) &
            (dataframe["volume"] > 0)
        )
        dataframe.loc[long_exit, ["exit_long", "exit_tag"]] = (1, "trend_flip_long")

        if self.can_short:
            short_exit = (
                (dataframe["ema_fast"] > dataframe["ema_slow"]) &
                (dataframe["volume"] > 0)
            )
            dataframe.loc[short_exit, ["exit_short", "exit_tag"]] = (1, "trend_flip_short")

        return dataframe
'''


def compile_strategy(genome: StrategyGenome) -> str:
    """Generated strategies expose buy-space IntParameters (entry RSI thresholds).

    The hyperopt ranges are confined to the genome neighbourhood (long_max
    20..45, short_min 55..80 — DECISIONS D-018): with the original wide ranges
    (1..49 / 51..99) hyperopt drifted into degenerate sparse-entry params
    (e.g. ``buy_rsi_long_max=2``, 39 trades / 3 years). roi/stoploss spaces use
    freqtrade defaults; the sell space is intentionally unused (exits are
    signal flips).
    """
    genome.validate()
    source = STRATEGY_TEMPLATE.format(
        class_name=genome.class_name,
        timeframe=genome.timeframe,
        can_short=repr(genome.can_short),
        startup_candle_count=max(genome.ema_slow + 20, 300),
        stoploss=genome.stoploss,
        ema_fast=genome.ema_fast,
        ema_slow=genome.ema_slow,
        rsi_period=genome.rsi_period,
        rsi_long_max=genome.rsi_long_max,
        rsi_short_min=genome.rsi_short_min,
    )
    ast.parse(source)
    return source


def write_strategy(genome: StrategyGenome, output_dir: Path) -> Path:
    output_dir.mkdir(parents=True, exist_ok=True)
    path = output_dir / f"{genome.class_name}.py"
    path.write_text(compile_strategy(genome), encoding="utf-8")
    return path
