"""Integration tests for UI strategy workflow."""

import pytest
from PyQt6.QtWidgets import QApplication
from unittest.mock import Mock, patch
from src.ui.widgets.strategy_manager import StrategyManagerWidget, StrategyDialog
from src.core.database import Strategy


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


@pytest.fixture
def strategy_manager(app):
    """Create StrategyManagerWidget."""
    return StrategyManagerWidget()


def test_strategy_creation_workflow(strategy_manager, app):
    """Test creating strategy via UI."""
    # Mock database
    with patch.object(strategy_manager.db, 'get_session') as mock_session:
        mock_session.return_value.__enter__.return_value.add = Mock()
        mock_session.return_value.__enter__.return_value.commit = Mock()
        
        # Simulate add strategy
        dialog = StrategyDialog(strategy_manager)
        dialog.name_input.setText("Test Strategy")
        dialog.symbol_input.setText("BTC/USD")
        dialog.type_combo.setCurrentText("rsi")
        
        # Would need to set exchange combo data
        # For now, just verify dialog structure
        assert dialog.name_input.text() == "Test Strategy"
        dialog.close()


def test_strategy_table_population(strategy_manager):
    """Test strategies table is populated from database."""
    # Mock database query
    mock_strategy = Mock(spec=Strategy)
    mock_strategy.id = 1
    mock_strategy.name = "Test Strategy"
    mock_strategy.strategy_type = "rsi"
    mock_strategy.parameters = {"symbol": "BTC/USD"}
    mock_strategy.enabled = True
    
    with patch.object(strategy_manager.db, 'get_session') as mock_session:
        mock_query = Mock()
        mock_query.all.return_value = [mock_strategy]
        mock_session.return_value.__enter__.return_value.query.return_value.filter_by.return_value.all = Mock(return_value=[mock_strategy])
        mock_session.return_value.__enter__.return_value.query.return_value.all = Mock(return_value=[mock_strategy])
        
        strategy_manager._refresh_strategies()
        
        # Verify table has data
        # Note: Actual implementation would verify table contents
