# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

import sys
import types
import warnings

from glide.glide import (
    ClusterScanCursor,
    Script,
    get_min_compressed_size,
)
from glide_shared import (
    ALL_CHANNELS,
    ALL_PATTERNS,
    ALL_SHARDED_CHANNELS,
    OK,
    TOK,
    AdvancedGlideClientConfiguration,
    AdvancedGlideClusterClientConfiguration,
    AggregationType,
    AllNodes,
    AllPrimaries,
    BackoffStrategy,
    Batch,
    BatchOptions,
    BatchRetryStrategy,
    BitEncoding,
    BitFieldGet,
    BitFieldIncrBy,
    BitFieldOffset,
    BitFieldOverflow,
    BitFieldSet,
    BitFieldSubCommands,
    BitmapIndexType,
    BitOffset,
    BitOffsetMultiplier,
    BitOverflowControl,
    BitwiseOperation,
    ByAddressRoute,
    ClosingError,
    ClusterBatch,
    ClusterBatchOptions,
    ClusterTransaction,
    CompressionBackend,
    CompressionConfiguration,
    ConditionalChange,
    ConfigurationError,
    ConnectionError,
    DataType,
    DistanceMetricType,
    ExclusiveIdBound,
    ExecAbortError,
    ExpireOptions,
    ExpiryGetEx,
    ExpirySet,
    ExpiryType,
    ExpiryTypeGetEx,
    Field,
    FieldType,
    FlushMode,
    FtAggregateApply,
    FtAggregateClause,
    FtAggregateFilter,
    FtAggregateGroupBy,
    FtAggregateLimit,
    FtAggregateOptions,
    FtAggregateReducer,
    FtAggregateResponse,
    FtAggregateSortBy,
    FtAggregateSortProperty,
    FtCreateOptions,
    FtInfoResponse,
    FtProfileOptions,
    FtProfileResponse,
    FtSearchLimit,
    FtSearchOptions,
    FtSearchResponse,
    FunctionRestorePolicy,
    GeoSearchByBox,
    GeoSearchByRadius,
    GeoSearchCount,
    GeospatialData,
    GeoUnit,
    GlideClientConfiguration,
    GlideClusterClientConfiguration,
    GlideError,
    HashFieldConditionalChange,
    IamAuthConfig,
    IdBound,
    InfBound,
    InfoSection,
    InsertPosition,
    JsonArrIndexOptions,
    JsonArrPopOptions,
    JsonGetOptions,
    LexBoundary,
    Limit,
    ListDirection,
    MaxId,
    MinId,
    NodeAddress,
    NumericField,
    ObjectType,
    OffsetOptions,
    OnlyIfEqual,
    OpenTelemetryConfig,
    OpenTelemetryMetricsConfig,
    OpenTelemetryTracesConfig,
    OrderBy,
    PeriodicChecksManualInterval,
    PeriodicChecksStatus,
    ProtocolVersion,
    PubSubMsg,
    QueryType,
    RandomNode,
    RangeByIndex,
    RangeByLex,
    RangeByScore,
    ReadFrom,
    RequestError,
    ReturnField,
    Route,
    ScoreBoundary,
    ScoreFilter,
    ServerCredentials,
    ServiceType,
    SignedEncoding,
    SlotIdRoute,
    SlotKeyRoute,
    SlotType,
    StreamAddOptions,
    StreamClaimOptions,
    StreamGroupOptions,
    StreamPendingOptions,
    StreamRangeBound,
    StreamReadGroupOptions,
    StreamReadOptions,
    StreamTrimOptions,
    TagField,
    TBatch,
    TClusterResponse,
    TEncodable,
    TextField,
    TFunctionListResponse,
    TFunctionStatsFullResponse,
    TFunctionStatsSingleNodeResponse,
    TimeoutError,
    TJsonResponse,
    TJsonUniversalResponse,
    TlsAdvancedConfiguration,
    Transaction,
    TResult,
    TrimByMaxLen,
    TrimByMinId,
    TSingleNodeRoute,
    TXInfoStreamFullResponse,
    TXInfoStreamResponse,
    UnsignedEncoding,
    UpdateOptions,
    VectorAlgorithm,
    VectorField,
    VectorFieldAttributes,
    VectorFieldAttributesFlat,
    VectorFieldAttributesHnsw,
    VectorType,
    json_batch,
)

from .async_commands import (
    ft,
    glide_json,
)
from .glide_client import GlideClient, GlideClusterClient, TGlideClient
from .logger import Level as LogLevel
from .logger import Logger
from .opentelemetry import OpenTelemetry

_glide_module = sys.modules[__name__]

_legacy_modules = [
    "glide.exceptions",
    "glide.config",
    "glide.constants",
    "glide.routes",
    "glide.async_commands",
    "glide.async_commands.batch",
    "glide.async_commands.batch_options",
    "glide.async_commands.bitmap",
    "glide.async_commands.command_args",
    "glide.async_commands.server_modules",
    "glide.async_commands.server_modules.ft_options",
    "glide.async_commands.server_modules.ft_options.ft_aggregate_options",
    "glide.async_commands.server_modules.ft_options.ft_create_options",
    "glide.async_commands.server_modules.ft_options.ft_profile_options",
    "glide.async_commands.server_modules.ft_options.ft_search_options",
    "glide.async_commands.server_modules.glide_json",
    "glide.async_commands.sorted_set",
    "glide.async_commands.stream",
]


class _LegacyModule(types.ModuleType):
    """Proxy for a deprecated module that warns when first accessed."""

    def __init__(self, name):
        super().__init__(name)
        self._warned = False

    def __getattr__(self, name):
        if not self._warned:
            warnings.warn(
                f"Importing from '{self.__name__}' is deprecated. "
                f"Please import directly from 'glide' instead.",
                DeprecationWarning,
                stacklevel=2,
            )
            self._warned = True

        # Access the attribute from the real top-level glide module
        return getattr(_glide_module, name)


# Replace old modules with lazy proxy modules
for old_name in _legacy_modules:
    if old_name not in sys.modules:
        sys.modules[old_name] = _LegacyModule(old_name)

__all__ = [
    # Client
    "TGlideClient",
    "GlideClient",
    "GlideClusterClient",
    # Internal utilities
    "get_min_compressed_size",
    "Batch",
    "ClusterBatch",
    "ClusterTransaction",
    "Transaction",
    "TBatch",
    # Batch Options
    "BatchOptions",
    "BatchRetryStrategy",
    "ClusterBatchOptions",
    # Config
    "AdvancedGlideClientConfiguration",
    "AdvancedGlideClusterClientConfiguration",
    "GlideClientConfiguration",
    "GlideClusterClientConfiguration",
    "BackoffStrategy",
    "CompressionBackend",
    "CompressionConfiguration",
    "ReadFrom",
    "ServerCredentials",
    "ServiceType",
    "IamAuthConfig",
    "NodeAddress",
    "OpenTelemetry",
    "OpenTelemetryConfig",
    "OpenTelemetryMetricsConfig",
    "OpenTelemetryTracesConfig",
    "ProtocolVersion",
    "PeriodicChecksManualInterval",
    "PeriodicChecksStatus",
    "TlsAdvancedConfiguration",
    # Response
    "OK",
    "TClusterResponse",
    "TEncodable",
    "TFunctionListResponse",
    "TFunctionStatsFullResponse",
    "TFunctionStatsSingleNodeResponse",
    "TJsonResponse",
    "TJsonUniversalResponse",
    "TOK",
    "TResult",
    "TXInfoStreamFullResponse",
    "TXInfoStreamResponse",
    "FtAggregateResponse",
    "FtInfoResponse",
    "FtProfileResponse",
    "FtSearchResponse",
    # Commands
    "BitEncoding",
    "BitFieldGet",
    "BitFieldIncrBy",
    "BitFieldOffset",
    "BitFieldOverflow",
    "BitFieldSet",
    "BitFieldSubCommands",
    "BitmapIndexType",
    "BitOffset",
    "BitOffsetMultiplier",
    "BitOverflowControl",
    "BitwiseOperation",
    "OffsetOptions",
    "SignedEncoding",
    "UnsignedEncoding",
    "Script",
    "ScoreBoundary",
    "ConditionalChange",
    "OnlyIfEqual",
    "ExpireOptions",
    "ExpiryGetEx",
    "ExpirySet",
    "ExpiryType",
    "ExpiryTypeGetEx",
    "FlushMode",
    "FunctionRestorePolicy",
    "GeoSearchByBox",
    "GeoSearchByRadius",
    "GeoSearchCount",
    "GeoUnit",
    "GeospatialData",
    "HashFieldConditionalChange",
    "AggregationType",
    "InfBound",
    "InfoSection",
    "InsertPosition",
    "LexBoundary",
    "Limit",
    "ListDirection",
    "RangeByIndex",
    "RangeByLex",
    "RangeByScore",
    "ScoreFilter",
    "ObjectType",
    "OrderBy",
    "ExclusiveIdBound",
    "IdBound",
    "MaxId",
    "MinId",
    "StreamAddOptions",
    "StreamClaimOptions",
    "StreamGroupOptions",
    "StreamPendingOptions",
    "StreamReadGroupOptions",
    "StreamRangeBound",
    "StreamReadOptions",
    "StreamTrimOptions",
    "TrimByMaxLen",
    "TrimByMinId",
    "UpdateOptions",
    "ClusterScanCursor",
    # PubSub
    "PubSubMsg",
    "ALL_CHANNELS",
    "ALL_PATTERNS",
    "ALL_SHARDED_CHANNELS",
    # Json
    "glide_json",
    "json_batch",
    "JsonGetOptions",
    "JsonArrIndexOptions",
    "JsonArrPopOptions",
    # Logger
    "Logger",
    "LogLevel",
    # Routes
    "Route",
    "SlotType",
    "AllNodes",
    "AllPrimaries",
    "ByAddressRoute",
    "RandomNode",
    "SlotKeyRoute",
    "SlotIdRoute",
    "TSingleNodeRoute",
    # Exceptions
    "ClosingError",
    "ConfigurationError",
    "ConnectionError",
    "ExecAbortError",
    "GlideError",
    "RequestError",
    "TimeoutError",
    # Ft
    "ft",
    "DataType",
    "DistanceMetricType",
    "Field",
    "FieldType",
    "FtCreateOptions",
    "NumericField",
    "TagField",
    "TextField",
    "VectorAlgorithm",
    "VectorField",
    "VectorFieldAttributes",
    "VectorFieldAttributesFlat",
    "VectorFieldAttributesHnsw",
    "VectorType",
    "FtSearchLimit",
    "ReturnField",
    "FtSearchOptions",
    "FtAggregateApply",
    "FtAggregateFilter",
    "FtAggregateClause",
    "FtAggregateLimit",
    "FtAggregateOptions",
    "FtAggregateGroupBy",
    "FtAggregateReducer",
    "FtAggregateSortBy",
    "FtAggregateSortProperty",
    "FtProfileOptions",
    "QueryType",
]
