"""Tests for portfolio API endpoints."""

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

from backend.main import app


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


@pytest.fixture
def mock_portfolio_tracker():
    """Mock portfolio tracker."""
    tracker = Mock()
    tracker.get_current_portfolio.return_value = {
        "positions": [],
        "performance": {"total_return": 0.1},
        "timestamp": "2025-01-01T00:00:00"
    }
    tracker.get_portfolio_history.return_value = {
        "dates": ["2025-01-01"],
        "values": [1000.0],
        "pnl": [0.0]
    }
    return tracker


class TestGetCurrentPortfolio:
    """Tests for GET /api/portfolio/current."""
    
    @patch('backend.api.portfolio.get_portfolio_tracker')
    def test_get_current_portfolio_success(self, mock_get_tracker, client, mock_portfolio_tracker):
        """Test getting current portfolio."""
        mock_get_tracker.return_value = mock_portfolio_tracker
        
        response = client.get("/api/portfolio/current?paper_trading=true")
        
        assert response.status_code == 200
        data = response.json()
        assert "positions" in data
        assert "performance" in data
        assert "timestamp" in data


class TestGetPortfolioHistory:
    """Tests for GET /api/portfolio/history."""
    
    @patch('backend.api.portfolio.get_portfolio_tracker')
    def test_get_portfolio_history_success(self, mock_get_tracker, client, mock_portfolio_tracker):
        """Test getting portfolio history."""
        mock_get_tracker.return_value = mock_portfolio_tracker
        
        response = client.get("/api/portfolio/history?paper_trading=true&days=30")
        
        assert response.status_code == 200
        data = response.json()
        assert "dates" in data
        assert "values" in data
        assert "pnl" in data

