"""Async HTTP client that posts status updates back into ERP."""
from __future__ import annotations

import httpx

from ..config import get_settings
from ..logger import get_logger
from ..schemas import ErpStatusUpdate

log = get_logger(__name__)


class ErpClient:
    def __init__(self) -> None:
        s = get_settings()
        self._url = s.erp_status_endpoint
        self._timeout = s.erp_timeout
        self._headers = {
            "X-Callback-Token": s.erp_callback_token,
            "Content-Type": "application/json",
            "Accept": "application/json",
        }

    async def push_status(self, update: ErpStatusUpdate) -> None:
        log.info(
            "→ ERP POST %s job=%s tank=%s plate=%s status=%s",
            self._url, update.job_id, update.tank_id,
            update.licence_number, update.status,
        )
        async with httpx.AsyncClient(timeout=self._timeout) as c:
            r = await c.post(
                self._url,
                json=update.model_dump(mode="json"),
                headers=self._headers,
            )
            r.raise_for_status()
