"""Type stubs for BTOON Python library"""

from typing import Any, Dict, List, Optional, Union, BinaryIO, Iterator, AsyncIterator
from datetime import datetime
from decimal import Decimal as StdDecimal
import numpy as np
import pandas as pd

__version__: str

# Core types
BTOONValue = Union[
    None,
    bool,
    int,
    float,
    str,
    bytes,
    List['BTOONValue'],
    Dict[str, 'BTOONValue'],
    'Timestamp',
    'Decimal'
]

# Core functions
def encode(
    data: Any,
    compress: bool = False,
    auto_tabular: bool = True,
    use_enhanced: bool = True,
    algorithm: Optional[str] = None,
    level: Optional[int] = None
) -> bytes: ...

def decode(
    data: bytes,
    decompress: bool = False,
    use_enhanced: bool = True
) -> Any: ...

# Extended types
class Timestamp:
    seconds: int
    nanoseconds: int
    timezone_offset: Optional[int]
    
    def __init__(self, seconds: int = 0, nanoseconds: int = 0, timezone_offset: Optional[int] = None) -> None: ...
    
    @classmethod
    def now(cls) -> 'Timestamp': ...
    
    @classmethod
    def from_datetime(cls, dt: datetime) -> 'Timestamp': ...
    
    @classmethod
    def from_microseconds(cls, microseconds: int) -> 'Timestamp': ...
    
    @classmethod
    def from_milliseconds(cls, milliseconds: int) -> 'Timestamp': ...
    
    def to_datetime(self) -> datetime: ...
    def to_microseconds(self) -> int: ...
    def to_milliseconds(self) -> int: ...
    def to_seconds_double(self) -> float: ...
    
    def __eq__(self, other: object) -> bool: ...
    def __lt__(self, other: 'Timestamp') -> bool: ...
    def __le__(self, other: 'Timestamp') -> bool: ...
    def __gt__(self, other: 'Timestamp') -> bool: ...
    def __ge__(self, other: 'Timestamp') -> bool: ...

class Decimal:
    def __init__(self, value: Union[str, int, float, StdDecimal]) -> None: ...
    
    @classmethod
    def from_float(cls, value: float, precision: int = 6) -> 'Decimal': ...
    
    def to_decimal(self) -> StdDecimal: ...
    def to_float(self) -> float: ...
    
    def __add__(self, other: Union['Decimal', int, float]) -> 'Decimal': ...
    def __sub__(self, other: Union['Decimal', int, float]) -> 'Decimal': ...
    def __mul__(self, other: Union['Decimal', int, float]) -> 'Decimal': ...
    def __truediv__(self, other: Union['Decimal', int, float]) -> 'Decimal': ...
    def __eq__(self, other: object) -> bool: ...
    def __lt__(self, other: Union['Decimal', int, float]) -> bool: ...
    def __le__(self, other: Union['Decimal', int, float]) -> bool: ...
    def __gt__(self, other: Union['Decimal', int, float]) -> bool: ...
    def __ge__(self, other: Union['Decimal', int, float]) -> bool: ...

class Currency:
    code: str
    amount: Decimal
    
    def __init__(self, code: str, amount: Union[Decimal, str, int, float]) -> None: ...

class Percentage:
    value: Decimal
    
    def __init__(self, value: Union[str, int, float]) -> None: ...
    def to_decimal(self) -> Decimal: ...
    def to_float(self) -> float: ...

# Enhanced encoder/decoder
class EnhancedEncoder:
    def encode(
        self,
        data: Any,
        compress: bool = False,
        auto_tabular: bool = True,
        algorithm: Optional[str] = None,
        level: Optional[int] = None
    ) -> bytes: ...

class EnhancedDecoder:
    def decode(self, data: bytes, decompress: bool = False) -> Any: ...

# Streaming
class StreamEncoder:
    def encode_chunk(self, obj: Any) -> bytes: ...
    def finalize(self) -> bytes: ...

class StreamDecoder:
    def decode_chunk(self, chunk: bytes) -> List[Any]: ...

class AsyncStreamEncoder:
    async def encode(self, obj: Any) -> bytes: ...
    async def finalize(self) -> bytes: ...

class AsyncStreamDecoder:
    async def decode(self, chunk: bytes) -> Any: ...
    def __aiter__(self) -> AsyncIterator[Any]: ...

# File operations
class BTOONFile:
    def __init__(self, path: str, mode: str = 'r') -> None: ...
    def __enter__(self) -> 'BTOONFile': ...
    def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None: ...
    def encode(self, obj: Any) -> None: ...
    def decode(self) -> Any: ...
    def __iter__(self) -> Iterator[Any]: ...

def open_btoon(path: str, mode: str = 'r') -> BTOONFile: ...

async def async_stream(path: str, mode: str = 'r') -> Union[AsyncStreamEncoder, AsyncStreamDecoder]: ...

# NumPy integration
def from_numpy(
    array: np.ndarray,
    compress: bool = False
) -> bytes: ...

def to_numpy(data: bytes) -> np.ndarray: ...

# Pandas integration
def from_dataframe(
    df: pd.DataFrame,
    compress: bool = True
) -> bytes: ...

def to_dataframe(data: bytes) -> pd.DataFrame: ...
