from fastapi.testclient import TestClient
from src.fmatch.saas.main import app
from src.fmatch.saas.api.v1 import deps as v1_deps


client = TestClient(app)


def test_v1_headers_present(monkeypatch):
    class Ctx:
        api_key_prefix = 'k'
        account_id = 'acct'
        tier = 'free'
        qps_limit = 2

    app.dependency_overrides[v1_deps.require_api_key] = lambda: Ctx()

    # Bypass engine path by stubbing PublicMatchService
    from src.fmatch.saas.services.public_match_service import PublicMatchService

    monkeypatch.setattr(PublicMatchService, "_is_sync", lambda self, body: True)

    async def fake_sync(self, body, profile):
        return {
            "best_match": {"id": "acc_1"},
            "confidence": 0.9,
            "explanations": [],
            "all_scores": [0.9],
            "recommendation": "merge",
            "decision_id": "dec_test",
        }

    monkeypatch.setattr(PublicMatchService, "_sync_match", fake_sync)

    r = client.post(
        '/api/v1/match',
        headers={'X-API-Key': 'dummy'},
        json={'profile': 'balanced', 'source': {'x': 1}, 'candidates': [{'y': 2}]}
    )
    assert r.status_code == 200
    assert r.headers.get('X-Foundry-Request-Id')
    assert r.headers.get('X-RateLimit-Limit') is not None
    assert r.headers.get('X-RateLimit-Remaining') is not None

