"""
Data models for DataManager operations.
"""

import polars as pl
from dataclasses import dataclass
from pathlib import Path
from typing import Iterator, Optional


@dataclass
class DataOperationResult:
    """Result from data operations (technical metrics only)"""

    output_location: Path
    processing_time_seconds: float
    peak_memory_usage_gb: float
    data_size_gb: float
    optimization_strategy: str
    file_count: int
    compression_ratio: Optional[float] = None

    def performance_summary(self) -> str:
        """Human-readable performance summary"""
        return (
            f"Processing: {self.processing_time_seconds:.1f}s, "
            f"Memory: {self.peak_memory_usage_gb:.1f}GB, "
            f"Strategy: {self.optimization_strategy}"
        )


@dataclass
class PerformanceMetrics:
    """Technical metrics from data operations (no business intelligence)"""

    processing_time_seconds: float
    peak_memory_usage_gb: float
    optimization_strategy: str
    memory_efficiency_score: float
    storage_compression_ratio: Optional[float] = None

    def performance_summary(self) -> str:
        """Human-readable performance summary"""
        return (
            f"Processing: {self.processing_time_seconds:.1f}s, "
            f"Memory: {self.peak_memory_usage_gb:.1f}GB, "
            f"Strategy: {self.optimization_strategy}"
        )


class DataStream:
    """Abstraction for data streaming operations"""

    def __init__(self, source: Path, lazy_df: Optional[pl.LazyFrame] = None):
        self.source = source
        self._lazy_df = lazy_df

    def collect(self) -> pl.DataFrame:
        """Collect the entire stream into memory"""
        if self._lazy_df is not None:
            return self._lazy_df.collect()

        # Load from source
        if self.source.suffix == ".parquet":
            return pl.read_parquet(self.source)
        elif self.source.is_dir():
            # Scan directory for parquet files
            parquet_files = list(self.source.glob("**/*.parquet"))
            if parquet_files:
                return pl.read_parquet(parquet_files)

        raise ValueError(f"Unsupported data source: {self.source}")

    def lazy(self) -> pl.LazyFrame:
        """Get lazy frame for efficient operations"""
        if self._lazy_df is not None:
            return self._lazy_df

        if self.source.suffix == ".parquet":
            return pl.scan_parquet(self.source)
        elif self.source.is_dir():
            parquet_files = list(self.source.glob("**/*.parquet"))
            if parquet_files:
                return pl.scan_parquet(parquet_files)

        raise ValueError(f"Unsupported data source: {self.source}")

    def __iter__(self) -> Iterator[pl.DataFrame]:
        """Iterate over chunks of data"""
        # Simple implementation - will be enhanced in future
        yield self.collect()
