"""Tests for strategies API endpoints."""

import pytest
from unittest.mock import Mock, patch, MagicMock
from fastapi.testclient import TestClient

from backend.main import app
from src.core.database import Strategy


@pytest.fixture
def client():
    """Test client fixture."""
    return TestClient(app)


@pytest.fixture
def mock_strategy_registry():
    """Mock strategy registry."""
    registry = Mock()
    registry.list_available.return_value = ["RSIStrategy", "MACDStrategy"]
    return registry


@pytest.fixture
def mock_strategy():
    """Mock strategy object."""
    strategy = Mock(spec=Strategy)
    strategy.id = 1
    strategy.name = "Test Strategy"
    strategy.type = "RSIStrategy"
    strategy.status = "active"  # Use string instead of enum
    strategy.symbol = "BTC/USD"
    strategy.params = {}
    return strategy



class TestListStrategies:
    """Tests for GET /api/strategies."""
    
    @patch('backend.api.strategies.get_db')
    def test_list_strategies_success(self, mock_get_db, client):
        """Test listing strategies."""
        mock_db = Mock()
        mock_session = Mock()
        mock_db.get_session.return_value = mock_session
        mock_get_db.return_value = mock_db
        
        mock_session.query.return_value.all.return_value = []
        
        response = client.get("/api/strategies")
        
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)


class TestGetStrategy:
    """Tests for GET /api/strategies/{strategy_id}."""
    
    @patch('backend.api.strategies.get_db')
    def test_get_strategy_success(self, mock_get_db, client, mock_strategy):
        """Test getting strategy by ID."""
        mock_db = Mock()
        mock_session = Mock()
        mock_db.get_session.return_value = mock_session
        mock_get_db.return_value = mock_db
        
        mock_session.query.return_value.filter_by.return_value.first.return_value = mock_strategy
        
        response = client.get("/api/strategies/1")
        
        assert response.status_code == 200
        data = response.json()
        assert data["id"] == 1
    
    @patch('backend.api.strategies.get_db')
    def test_get_strategy_not_found(self, mock_get_db, client):
        """Test getting non-existent strategy."""
        mock_db = Mock()
        mock_session = Mock()
        mock_db.get_session.return_value = mock_session
        mock_get_db.return_value = mock_db
        
        mock_session.query.return_value.filter_by.return_value.first.return_value = None
        
        response = client.get("/api/strategies/999")
        
        assert response.status_code == 404
        assert "Strategy not found" in response.json()["detail"]


class TestListAvailableStrategyTypes:
    """Tests for GET /api/strategies/types."""
    
    @patch('backend.api.strategies.get_strategy_registry')
    def test_list_strategy_types_success(self, mock_get_registry, client, mock_strategy_registry):
        """Test listing available strategy types."""
        mock_get_registry.return_value = mock_strategy_registry
        
        response = client.get("/api/strategies/types")
        
        assert response.status_code == 200
        data = response.json()
        assert isinstance(data, list)
        assert "RSIStrategy" in data

