"""Tests for DCA strategy."""

import pytest
from decimal import Decimal
from datetime import datetime, timedelta
from src.strategies.dca.dca_strategy import DCAStrategy


def test_dca_strategy_initialization():
    """Test DCA strategy initializes correctly."""
    strategy = DCAStrategy("Test DCA", {"amount": 10, "interval": "daily"})
    assert strategy.name == "Test DCA"
    assert strategy.amount == Decimal("10")
    assert strategy.interval == "daily"


def test_dca_daily_interval():
    """Test DCA with daily interval."""
    strategy = DCAStrategy("Daily DCA", {"amount": 10, "interval": "daily"})
    assert strategy.interval_delta == timedelta(days=1)


def test_dca_weekly_interval():
    """Test DCA with weekly interval."""
    strategy = DCAStrategy("Weekly DCA", {"amount": 10, "interval": "weekly"})
    assert strategy.interval_delta == timedelta(weeks=1)


def test_dca_monthly_interval():
    """Test DCA with monthly interval."""
    strategy = DCAStrategy("Monthly DCA", {"amount": 10, "interval": "monthly"})
    assert strategy.interval_delta == timedelta(days=30)


def test_dca_signal_generation():
    """Test DCA generates buy signals."""
    strategy = DCAStrategy("Test DCA", {"amount": 10, "interval": "daily"})
    strategy.last_purchase_time = None
    
    signal = strategy.on_tick("BTC/USD", Decimal("100"), "1h", {})
    assert signal is not None
    assert signal.signal_type.value == "buy"
    assert signal.quantity == Decimal("0.1")  # 10 / 100


def test_dca_interval_respect():
    """Test DCA respects interval timing."""
    strategy = DCAStrategy("Test DCA", {"amount": 10, "interval": "daily"})
    strategy.last_purchase_time = datetime.utcnow() - timedelta(hours=12)
    
    signal = strategy.on_tick("BTC/USD", Decimal("100"), "1h", {})
    assert signal is None  # Should not generate signal yet
