from __future__ import annotations

import pytest

from crypto_spot_bot.config import FIXED_SPOT_SYMBOLS, load_settings


def test_live_mode_requires_explicit_unlock(tmp_path, monkeypatch) -> None:
    for key in (
        "TRADING_MODE",
        "ENABLE_LIVE_TRADING",
        "LIVE_TRADING_CONFIRM",
        "BYBIT_API_KEY",
        "BYBIT_API_SECRET",
    ):
        monkeypatch.delenv(key, raising=False)
    env_file = tmp_path / ".env"
    env_file.write_text("TRADING_MODE=live\n", encoding="utf-8")

    with pytest.raises(ValueError):
        load_settings(env_file)


def test_fast_trading_env_sets_effective_intervals(tmp_path, monkeypatch) -> None:
    for key in (
        "TRADING_MODE",
        "FAST_TRADING_ENABLED",
        "FAST_LOOP_INTERVAL_SECONDS",
        "FAST_ENTRY_COOLDOWN_SECONDS",
        "MAX_ENTRIES_PER_MINUTE",
    ):
        monkeypatch.delenv(key, raising=False)
    env_file = tmp_path / ".env"
    env_file.write_text(
        "\n".join(
            [
                "TRADING_MODE=paper",
                "FAST_TRADING_ENABLED=true",
                "FAST_LOOP_INTERVAL_SECONDS=0.75",
                "FAST_ENTRY_COOLDOWN_SECONDS=12",
                "MAX_ENTRIES_PER_MINUTE=4",
            ]
        ),
        encoding="utf-8",
    )

    settings = load_settings(env_file)

    assert settings.fast_trading_enabled is True
    assert settings.effective_loop_interval_seconds == 0.75
    assert settings.effective_entry_cooldown_seconds == 12
    assert settings.max_entries_per_minute == 4


def test_symbol_risk_guard_can_be_disabled(tmp_path, monkeypatch) -> None:
    monkeypatch.delenv("RISK_SYMBOL_GUARD_ENABLED", raising=False)
    monkeypatch.setenv("TRADING_MODE", "paper")
    env_file = tmp_path / ".env"
    env_file.write_text(
        "TRADING_MODE=paper\nRISK_SYMBOL_GUARD_ENABLED=false\n",
        encoding="utf-8",
    )

    settings = load_settings(env_file)

    assert settings.risk_guard_enabled is True
    assert settings.risk_symbol_guard_enabled is False


def test_llm_advisor_is_disabled_by_default(tmp_path, monkeypatch) -> None:
    monkeypatch.delenv("LLM_ADVISOR_ENABLED", raising=False)
    monkeypatch.setenv("TRADING_MODE", "paper")
    env_file = tmp_path / ".env"
    env_file.write_text("TRADING_MODE=paper\n", encoding="utf-8")

    settings = load_settings(env_file)

    assert settings.llm_advisor_enabled is False


def test_default_symbols_are_fixed_trend_pairs(tmp_path, monkeypatch) -> None:
    monkeypatch.delenv("AUTO_SELECT_SYMBOLS", raising=False)
    monkeypatch.delenv("TOP_SYMBOLS_COUNT", raising=False)
    monkeypatch.delenv("SYMBOLS", raising=False)
    monkeypatch.delenv("STRATEGY_MODE", raising=False)
    monkeypatch.setenv("TRADING_MODE", "paper")
    env_file = tmp_path / ".env"
    env_file.write_text("TRADING_MODE=paper\nSYMBOLS=\n", encoding="utf-8")

    settings = load_settings(env_file)

    assert settings.auto_select_symbols is False
    assert settings.top_symbols_count == len(FIXED_SPOT_SYMBOLS)
    assert settings.symbols == FIXED_SPOT_SYMBOLS
    assert settings.strategy_mode == "torch_forecast"
    assert settings.base_interval == "60"
    assert settings.trend_interval == "D"
    assert settings.risk_per_trade_percent == 0.01
    assert settings.time_series_forecast_enabled is True


def test_torch_forecast_keeps_configured_symbol_selection(tmp_path, monkeypatch) -> None:
    for key in (
        "AUTO_SELECT_SYMBOLS",
        "TOP_SYMBOLS_COUNT",
        "SYMBOLS",
        "STRATEGY_MODE",
        "TIME_SERIES_FORECAST_ENABLED",
    ):
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv("TRADING_MODE", "paper")
    env_file = tmp_path / ".env"
    env_file.write_text(
        "\n".join(
            [
                "TRADING_MODE=paper",
                "STRATEGY_MODE=torch_forecast",
                "AUTO_SELECT_SYMBOLS=true",
                "TOP_SYMBOLS_COUNT=9",
                "SYMBOLS=DOGEUSDT,XRPUSDT",
            ]
        ),
        encoding="utf-8",
    )

    settings = load_settings(env_file)

    assert settings.auto_select_symbols is True
    assert settings.top_symbols_count == 9
    assert settings.symbols == ("DOGEUSDT", "XRPUSDT")
    assert settings.time_series_forecast_enabled is True


def test_auto_select_uses_empty_symbol_list(tmp_path, monkeypatch) -> None:
    for key in ("AUTO_SELECT_SYMBOLS", "TOP_SYMBOLS_COUNT", "SYMBOLS", "STRATEGY_MODE"):
        monkeypatch.delenv(key, raising=False)
    monkeypatch.setenv("TRADING_MODE", "paper")
    env_file = tmp_path / ".env"
    env_file.write_text(
        "\n".join(
            [
                "TRADING_MODE=paper",
                "STRATEGY_MODE=torch_forecast",
                "AUTO_SELECT_SYMBOLS=true",
                "TOP_SYMBOLS_COUNT=12",
                "SYMBOLS=",
            ]
        ),
        encoding="utf-8",
    )

    settings = load_settings(env_file)

    assert settings.auto_select_symbols is True
    assert settings.top_symbols_count == 12
    assert settings.symbols == ()
