"""ActiWAPI Python client (starter)."""
from __future__ import annotations

import os
from typing import Any, Dict, Optional

import requests


class ActiwapiClient:
    def __init__(self, api_key: Optional[str] = None, base_url: str = "http://localhost:4000/api/external") -> None:
        self.api_key = api_key or os.environ.get("ACTIWAPI_API_KEY", "")
        self.base_url = base_url.rstrip("/")
        self.session = requests.Session()
        self.session.headers.update({
            "Content-Type": "application/json",
            "X-API-Key": self.api_key,
        })

    def request(self, method: str, path: str, json: Optional[Dict[str, Any]] = None) -> Any:
        response = self.session.request(method, f"{self.base_url}{path}", json=json, timeout=30)
        response.raise_for_status()
        return response.json() if response.text else None

    def send_text(self, session_id: str, phone: str, text: str) -> Any:
        return self.request("POST", "/messages/send-text", {
            "sessionId": session_id,
            "phone": phone,
            "text": text,
        })
