"""
Light adapter layer to preserve existing third-party integration code while
moving API orchestration to Django.

Xero: one XeroService instance per Django Client so tokens + in-memory cache never cross tenants.
SimPRO: SimProClient is constructed per branch_id; branch credentials come from settings.BRANCHES
(which merges ClientBranch rows for the current request client).
"""

from functools import lru_cache
from typing import Optional


@lru_cache(maxsize=64)
def _xero_service_for_client(django_client_id: Optional[int]):
    from app.services.xero_service import XeroService

    return XeroService(django_client_id=django_client_id)


def get_xero_service():
    try:
        from integrations.client_context import get_current_client

        client = get_current_client()
    except Exception:
        client = None
    key = client.pk if client else None
    return _xero_service_for_client(key)


def get_simpro_client():
    from app.services.simpro_client import SimProClient

    return SimProClient


def get_sheets_service():
    from app.services.sheets_service import sheets_service

    return sheets_service
