"""Tests that APIClient forwards the real tablet IP via X-Forwarded-For."""
from unittest.mock import patch

from services.api_client import api_client


def _last_call_headers(mock):
    """Return the headers kwarg from the most recent requests.* call."""
    return mock.call_args.kwargs["headers"]


def test_get_forwards_real_client_ip(flask_app):
    with flask_app.test_request_context(
        "/", environ_base={"REMOTE_ADDR": "203.0.113.10"}
    ):
        with patch("services.api_client.requests.get") as mock_get:
            mock_get.return_value.ok = True
            mock_get.return_value.status_code = 200
            mock_get.return_value.json.return_value = {}
            api_client.get("/api/health")

        headers = _last_call_headers(mock_get)
        assert headers["X-Forwarded-For"] == "203.0.113.10"
        assert headers["X-Real-IP"] == "203.0.113.10"


def test_post_json_forwards_real_client_ip(flask_app):
    with flask_app.test_request_context(
        "/", environ_base={"REMOTE_ADDR": "198.51.100.42"}
    ):
        with patch("services.api_client.requests.post") as mock_post:
            mock_post.return_value.ok = True
            mock_post.return_value.status_code = 200
            mock_post.return_value.json.return_value = {}
            api_client.post("/api/foo", data={"x": 1})

        headers = _last_call_headers(mock_post)
        assert headers["X-Forwarded-For"] == "198.51.100.42"


def test_post_with_files_forwards_real_client_ip(flask_app):
    with flask_app.test_request_context(
        "/", environ_base={"REMOTE_ADDR": "198.51.100.77"}
    ):
        with patch("services.api_client.requests.post") as mock_post:
            mock_post.return_value.ok = True
            mock_post.return_value.status_code = 200
            mock_post.return_value.json.return_value = {}
            api_client.post(
                "/api/files/upload", data={}, files={"file": ("x.txt", b"data")}
            )

        headers = _last_call_headers(mock_post)
        assert headers["X-Forwarded-For"] == "198.51.100.77"
        assert headers["X-Real-IP"] == "198.51.100.77"


def test_real_client_ip_returns_none_outside_request_context(flask_app):
    """Background workers without a Flask request must NOT inject a fake IP."""
    from services.api_client import APIClient

    with flask_app.app_context():
        assert APIClient._real_client_ip() is None


def test_proxy_fix_is_installed(flask_app):
    """ProxyFix must wrap wsgi_app so REMOTE_ADDR is rewritten in production."""
    from werkzeug.middleware.proxy_fix import ProxyFix

    assert isinstance(flask_app.wsgi_app, ProxyFix)
