"""Tests for trend filter functionality."""

import pytest
import pandas as pd
from decimal import Decimal
from src.strategies.base import BaseStrategy, StrategySignal, SignalType
from src.strategies.technical.rsi_strategy import RSIStrategy


class TestTrendFilter:
    """Tests for trend filter in BaseStrategy."""
    
    @pytest.fixture
    def strategy(self):
        """Create strategy instance with trend filter enabled."""
        strategy = RSIStrategy(
            name="test_rsi_with_filter",
            parameters={'use_trend_filter': True}
        )
        return strategy
    
    @pytest.fixture
    def ohlcv_data(self):
        """Create sample OHLCV data."""
        dates = pd.date_range(start='2025-01-01', periods=50, freq='1H')
        base_price = 50000
        return pd.DataFrame({
            'high': [base_price + 100 + i * 10 for i in range(50)],
            'low': [base_price - 100 + i * 10 for i in range(50)],
            'close': [base_price + i * 10 for i in range(50)],
            'open': [base_price - 50 + i * 10 for i in range(50)],
            'volume': [1000.0] * 50
        }, index=dates)
    
    def test_trend_filter_method_exists(self, strategy):
        """Test that apply_trend_filter method exists."""
        assert hasattr(strategy, 'apply_trend_filter')
        assert callable(getattr(strategy, 'apply_trend_filter'))
    
    def test_trend_filter_insufficient_data(self, strategy):
        """Test trend filter with insufficient data."""
        signal = StrategySignal(
            signal_type=SignalType.BUY,
            symbol="BTC/USD",
            strength=0.8,
            price=Decimal("50000")
        )
        
        insufficient_data = pd.DataFrame({
            'high': [51000],
            'low': [49000],
            'close': [50000]
        })
        
        # Should allow signal when insufficient data
        result = strategy.apply_trend_filter(signal, insufficient_data)
        assert result is not None
    
    def test_trend_filter_none_data(self, strategy):
        """Test trend filter with None data."""
        signal = StrategySignal(
            signal_type=SignalType.BUY,
            symbol="BTC/USD",
            strength=0.8,
            price=Decimal("50000")
        )
        
        # Should allow signal when no data provided
        result = strategy.apply_trend_filter(signal, None)
        assert result is not None
    
    def test_trend_filter_when_disabled(self, strategy):
        """Test that trend filter doesn't filter when disabled."""
        strategy_no_filter = RSIStrategy(
            name="test_rsi_no_filter",
            parameters={'use_trend_filter': False}
        )
        
        signal = StrategySignal(
            signal_type=SignalType.BUY,
            symbol="BTC/USD",
            strength=0.8,
            price=Decimal("50000")
        )
        
        result = strategy_no_filter.apply_trend_filter(signal, None)
        assert result == signal

