import os
from typing import Optional

from .base_client import BaseClient
from .exceptions import AnyFastError
from .resources import Chat, Image, Video, Asset


class AnyFast:
    """AnyFast SDK client.

    Args:
        api_key: API key. Defaults to ANYFAST_API_KEY environment variable.
        base_url: API base URL. Defaults to https://www.anyfast.ai.
        gateway_url: Gateway URL. Defaults to https://www.anyfast.ai.
        timeout: Request timeout in seconds. Defaults to 30.
    """

    def __init__(
        self,
        api_key: Optional[str] = None,
        base_url: str = "https://www.anyfast.ai",
        gateway_url: str = "https://www.anyfast.ai",
        timeout: float = 30.0,
    ):
        self.api_key = api_key or os.environ.get("ANYFAST_API_KEY", "")
        if not self.api_key:
            raise AnyFastError(
                "API key is required. Pass it via api_key parameter "
                "or set ANYFAST_API_KEY environment variable."
            )

        self._base_client = BaseClient(
            api_key=self.api_key,
            base_url=base_url,
            gateway_url=gateway_url,
            timeout=timeout,
        )

        self.chat = Chat(self._base_client)
        self.image = Image(self._base_client)
        self.video = Video(self._base_client)
        self.asset = Asset(self._base_client)
