"""Integration tests for UI backtest workflow."""

import pytest
from datetime import datetime, timedelta
from PyQt6.QtWidgets import QApplication
from unittest.mock import Mock, patch
from src.ui.widgets.backtest_view import BacktestViewWidget
from src.strategies.technical.rsi_strategy import RSIStrategy


@pytest.fixture
def app():
    """Create QApplication for tests."""
    if not QApplication.instance():
        return QApplication([])
    return QApplication.instance()


@pytest.fixture
def backtest_view(app):
    """Create BacktestViewWidget."""
    view = BacktestViewWidget()
    
    # Mock backtest engine
    mock_engine = Mock()
    mock_engine.run_backtest.return_value = {
        'total_return': 10.5,
        'sharpe_ratio': 1.2,
        'max_drawdown': -5.0,
        'win_rate': 0.55,
        'total_trades': 50,
        'final_value': 110.5,
        'trades': []
    }
    view.backtest_engine = mock_engine
    
    return view


def test_backtest_configuration(backtest_view):
    """Test backtest configuration form."""
    assert backtest_view.strategy_combo is not None
    assert backtest_view.symbol_input is not None
    assert backtest_view.start_date is not None
    assert backtest_view.end_date is not None


def test_backtest_results_display(backtest_view):
    """Test backtest results are displayed."""
    results = {
        'total_return': 10.5,
        'sharpe_ratio': 1.2,
        'max_drawdown': -5.0,
        'win_rate': 0.55,
        'total_trades': 50,
        'final_value': 110.5,
        'trades': []
    }
    
    backtest_view._display_results(results)
    
    # Verify metrics text contains results
    metrics_text = backtest_view.metrics_text.toPlainText()
    assert "10.5" in metrics_text  # Total return
    assert "1.2" in metrics_text  # Sharpe ratio
