"""
SAGE-TSDB: Time Series Database Component for SAGE

Provides efficient time series data storage, querying, and processing capabilities
for streaming and historical data analysis.

Note: SAGE TSDB is an independent PyPI package (isage-tsdb).
      pip install isage-tsdb

Import strategy (in priority order):
  1. Core types  (TimeSeriesDB, TimeSeriesData, TimeRange, QueryConfig, TimeSeriesIndex)
     — always from isage-tsdb PyPI package.
  2. Algorithm/service types (AggregationType, OutOfOrderStreamJoin, WindowAggregator,
     SageTSDBService …)
     — from isage-tsdb if the installed version exposes them (v0.1.7+),
       otherwise from the local python/ fallback layer.
"""

import warnings

# ---------------------------------------------------------------------------
# Step 1: Core DB types — from isage-tsdb PyPI package
# ---------------------------------------------------------------------------
_SAGE_TSDB_AVAILABLE = False
try:
    from sage_tsdb import (
        QueryConfig,
        TimeRange,
        TimeSeriesData,
        TimeSeriesDB,
        TimeSeriesIndex,
    )

    # Backward-compatibility alias
    SageTSDB = TimeSeriesDB
    _SAGE_TSDB_AVAILABLE = True
except ImportError as e:
    warnings.warn(
        f"SAGE TSDB not available: {e}\n"
        "Install with: pip install isage-tsdb\n"
        "Time series features will be unavailable.",
        UserWarning,
        stacklevel=2,
    )
    SageTSDB = None  # type: ignore[assignment]
    TimeSeriesDB = None  # type: ignore[assignment]
    TimeSeriesData = None  # type: ignore[assignment]
    QueryConfig = None  # type: ignore[assignment]
    TimeRange = None  # type: ignore[assignment]
    TimeSeriesIndex = None  # type: ignore[assignment]

# ---------------------------------------------------------------------------
# Step 2: Algorithm & service types
# Try the PyPI package first (available in isage-tsdb >= 0.1.7),
# then fall back to the local python/ implementation.
# ---------------------------------------------------------------------------
_algo_from_pypi = False
if _SAGE_TSDB_AVAILABLE:
    try:
        from sage_tsdb import (
            AggregationType,
            InterpolationType,
            OutOfOrderStreamJoin,
            SageTSDBService,
            SageTSDBServiceConfig,
            TimeSeriesAlgorithm,
            WindowAggregator,
        )

        _algo_from_pypi = True
    except ImportError:
        pass  # fall through to local python/ layer

if not _algo_from_pypi:
    try:
        from .python.algorithms import (
            OutOfOrderStreamJoin,
            TimeSeriesAlgorithm,
            WindowAggregator,
        )
        from .python.micro_service.sage_tsdb_service import (
            SageTSDBService,
            SageTSDBServiceConfig,
        )
        from .python.sage_tsdb import AggregationType, InterpolationType
    except ImportError:
        AggregationType = None  # type: ignore[assignment]
        InterpolationType = None  # type: ignore[assignment]
        TimeSeriesAlgorithm = None  # type: ignore[assignment]
        OutOfOrderStreamJoin = None  # type: ignore[assignment]
        WindowAggregator = None  # type: ignore[assignment]
        SageTSDBService = None  # type: ignore[assignment]
        SageTSDBServiceConfig = None  # type: ignore[assignment]

__all__ = [
    # Availability flag
    "_SAGE_TSDB_AVAILABLE",
    # Core DB + types (may be None if isage-tsdb not installed)
    "SageTSDB",  # backward-compat alias → TimeSeriesDB
    "TimeSeriesDB",
    "TimeSeriesData",
    "QueryConfig",
    "TimeRange",
    "TimeSeriesIndex",
    "AggregationType",
    "InterpolationType",
    # Algorithms
    "TimeSeriesAlgorithm",
    "OutOfOrderStreamJoin",
    "WindowAggregator",
    # Service
    "SageTSDBService",
    "SageTSDBServiceConfig",
]
