"""
ATS SDK - Main Client

The ATSClient is the entry point for all SDK operations.
"""

from typing import Optional
from .config import ATSConfig
from .resources.devices import DevicesResource
from .resources.projects import ProjectsResource
from .resources.organizations import OrganizationsResource
from .resources.vsat import VsatResource
from .resources.iot_messaging import IoTMessagingResource
from .resources.telemetry import TelemetryResource
from .resources.usage import UsageResource
from .http_client import HTTPClient


class ATSClient:
    """
    ATS Platform API Client
    
    Initialize with either an API key or OAuth2 token.
    
    Args:
        api_key: API key for authentication (project-level)
        access_token: OAuth2 access token (user-level)
        base_url: API base URL (default: production)
        config: Optional ATSConfig for advanced configuration
    
    Example:
        >>> client = ATSClient(api_key="ats_live_abc123...")
        >>> devices = client.devices.list(project_id="proj_xyz")
    """
    
    def __init__(
        self,
        api_key: Optional[str] = None,
        access_token: Optional[str] = None,
        base_url: Optional[str] = None,
        config: Optional[ATSConfig] = None,
    ):
        # Use provided config or create default
        self.config = config or ATSConfig(
            api_key=api_key,
            access_token=access_token,
            base_url=base_url,
        )
        
        # Initialize HTTP client
        self._http = HTTPClient(self.config)
        
        # Initialize resource clients
        self._devices = DevicesResource(self._http)
        self._projects = ProjectsResource(self._http)
        self._organizations = OrganizationsResource(self._http)
        self._vsat = VsatResource(self._http)
        self._iot_messaging = IoTMessagingResource(self._http)
        self._telemetry = TelemetryResource(self._http)
        self._usage = UsageResource(self._http)
    
    @property
    def devices(self) -> DevicesResource:
        """Device management operations"""
        return self._devices
    
    @property
    def projects(self) -> ProjectsResource:
        """Project management operations"""
        return self._projects
    
    @property
    def organizations(self) -> OrganizationsResource:
        """Organization management operations"""
        return self._organizations
    
    @property
    def vsat(self) -> VsatResource:
        """VSAT connectivity operations"""
        return self._vsat
    
    @property
    def iot_messaging(self) -> IoTMessagingResource:
        """IoT messaging operations"""
        return self._iot_messaging
    
    @property
    def telemetry(self) -> TelemetryResource:
        """Telemetry and analytics operations"""
        return self._telemetry
    
    @property
    def usage(self) -> UsageResource:
        """Usage and billing operations"""
        return self._usage
    
    def close(self):
        """Close the HTTP client session"""
        self._http.close()
    
    def __enter__(self):
        """Context manager support"""
        return self
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        """Context manager cleanup"""
        self.close()
