import pytest
from unittest.mock import MagicMock, patch
from flask import Flask
from backend.services.redfish_client import RedfishClient

@pytest.fixture
def app():
    app = Flask(__name__)
    app.config["REDFISH_TIMEOUT"] = 10
    app.config["REDFISH_VERIFY_SSL"] = True
    return app

@patch("backend.services.redfish_client.requests.Session")
def test_redfish_client_init_defaults(mock_session):
    """Test initialization with default values when no app context."""
    client = RedfishClient("1.2.3.4", "user", "pass")
    assert client.timeout == 15
    assert client.verify_ssl == False
    assert client.base_url == "https://1.2.3.4/redfish/v1"

@patch("backend.services.redfish_client.requests.Session")
def test_redfish_client_init_with_app_config(mock_session, app):
    """Test initialization picking up Flask app config."""
    with app.app_context():
        client = RedfishClient("1.2.3.4", "user", "pass")
        assert client.timeout == 10
        assert client.verify_ssl == True

@patch("backend.services.redfish_client.requests.Session")
def test_redfish_client_init_override(mock_session, app):
    """Test explicit arguments override config."""
    with app.app_context():
        client = RedfishClient("1.2.3.4", "user", "pass", timeout=30, verify_ssl=False)
        assert client.timeout == 30
        assert client.verify_ssl == False

@patch("backend.services.redfish_client.requests.Session")
def test_get_success(mock_session):
    """Test successful GET request."""
    mock_response = MagicMock()
    mock_response.json.return_value = {"key": "value"}
    mock_response.status_code = 200
    
    session_instance = mock_session.return_value
    session_instance.get.return_value = mock_response
    
    client = RedfishClient("1.2.3.4", "user", "pass")
    result = client.get("/Some/Endpoint")
    
    assert result == {"key": "value"}
    session_instance.get.assert_called_with("https://1.2.3.4/redfish/v1/Some/Endpoint", timeout=15)

@patch("backend.services.redfish_client.requests.Session")
def test_get_absolute_path(mock_session):
    """Test GET request with absolute path."""
    mock_response = MagicMock()
    mock_response.json.return_value = {}
    session_instance = mock_session.return_value
    session_instance.get.return_value = mock_response
    
    client = RedfishClient("1.2.3.4", "user", "pass")
    client.get("/redfish/v1/Managers/1")
    
    session_instance.get.assert_called_with("https://1.2.3.4/redfish/v1/Managers/1", timeout=15)
