# -*- coding: utf-8 -*-
"""
Progressive Filter Executor for FilterMate

Provides lazy loading and chunked filtering for large PostgreSQL datasets
with complex expressions. Optimizes memory usage and query performance.

Key Features:
- Two-phase filtering (bbox pre-filter + full predicate)
- Streaming cursor for memory-efficient result iteration
- Chunked ID retrieval to avoid massive IN clauses
- Adaptive strategy selection based on query complexity

Performance Benefits:
- 3-10x faster on complex expressions (two-phase filtering)
- 50-80% memory reduction (streaming instead of fetch-all)
- Reduced network overhead (chunked transfers)
- Better PostgreSQL query plan utilization

Usage:
    from ...core.strategies import (        ProgressiveFilterExecutor,
        TwoPhaseFilter,
        LazyResultIterator
    )

    # Two-phase filtering for complex expressions
    executor = TwoPhaseFilter(conn, layer_props)
    result_ids = executor.execute(expression, source_bbox)

    # Lazy iteration over large result sets
    with LazyResultIterator(conn, query, chunk_size=5000) as iterator:
        for id_batch in iterator:
            process_batch(id_batch)
"""

import time
import re
from typing import (
    Dict, List, Optional, Tuple, Generator, Any,
    Iterator, Callable, Union
)
from dataclasses import dataclass
from enum import Enum

from ...infrastructure.logging import get_logger

logger = get_logger(__name__)

# Centralized psycopg2 availability (v2.8.6 refactoring)
from ...infrastructure.database.postgresql_support import PSYCOPG2_AVAILABLE

# For backward compatibility
POSTGRESQL_AVAILABLE = PSYCOPG2_AVAILABLE

# Import psycopg2.sql if available
if PSYCOPG2_AVAILABLE:
    from psycopg2 import sql as psycopg2_sql
else:
    psycopg2_sql = None


class FilterStrategy(Enum):
    """Filter execution strategies based on query complexity and data size."""
    DIRECT = "direct"                    # Simple setSubsetString
    MATERIALIZED = "materialized"        # Materialized view with index
    TWO_PHASE = "two_phase"              # Bbox pre-filter + full predicate
    PROGRESSIVE = "progressive"          # Chunked streaming for very large results
    LAZY_CURSOR = "lazy_cursor"          # Server-side cursor streaming
    ATTRIBUTE_FIRST = "attribute_first"  # v2.5.10: Attribute filter before geometry
    MULTI_STEP = "multi_step"            # v2.5.10: Multi-step adaptive filtering


@dataclass
class FilterResult:
    """Result of a progressive filter operation."""
    success: bool
    feature_ids: Optional[List[int]] = None
    feature_count: int = 0
    strategy_used: FilterStrategy = FilterStrategy.DIRECT
    execution_time_ms: float = 0.0
    phases_executed: int = 1
    memory_saved_estimate_mb: float = 0.0
    error: Optional[str] = None

    # Detailed timing breakdown
    phase1_time_ms: float = 0.0  # Bbox/pre-filter phase
    phase2_time_ms: float = 0.0  # Full predicate phase

    # Statistics
    candidates_after_phase1: int = 0
    reduction_ratio: float = 0.0  # How much phase1 reduced the dataset


@dataclass
class LayerProperties:
    """PostgreSQL layer properties for filter execution."""
    schema: str = "public"
    table: str = ""
    geometry_column: str = "geom"
    primary_key: str = "id"
    srid: int = 4326
    estimated_feature_count: int = 0
    has_spatial_index: bool = True

    @classmethod
    def from_dict(cls, props: Dict) -> 'LayerProperties':
        """Create LayerProperties from dictionary."""
        return cls(
            schema=props.get('layer_schema', props.get('schema', 'public')),
            table=props.get('layer_table_name', props.get('table', '')),
            geometry_column=props.get('layer_geometry_field', props.get('geometry_column', 'geom')),
            primary_key=props.get('layer_pk', props.get('primary_key', 'id')),
            srid=props.get('layer_srid', props.get('srid', 4326)),
            estimated_feature_count=props.get('feature_count', 0),
            has_spatial_index=props.get('has_spatial_index', True)
        )


class LazyResultIterator:
    """
    Memory-efficient iterator for large PostgreSQL result sets.

    Uses server-side cursors to stream results in chunks, avoiding
    loading entire result sets into memory.

    Memory Comparison (1M rows, ~100 bytes per ID):
    - Standard fetchall(): ~100MB in memory
    - LazyResultIterator(chunk_size=5000): ~500KB in memory

    Usage:
        with LazyResultIterator(conn, query, chunk_size=5000) as iterator:
            for id_batch in iterator:
                # Process 5000 IDs at a time
                expression = build_in_clause(id_batch)
    """

    DEFAULT_CHUNK_SIZE = 5000

    def __init__(
        self,
        connection,
        query: str,
        params: Tuple = None,
        chunk_size: int = None,
        cursor_name: str = None
    ):
        """
        Initialize lazy result iterator.

        Args:
            connection: psycopg2 connection
            query: SQL query to execute
            params: Query parameters (optional)
            chunk_size: Number of rows per chunk (default: 5000)
            cursor_name: Name for server-side cursor (auto-generated if None)
        """
        self.connection = connection
        self.query = query
        self.params = params
        self.chunk_size = chunk_size or self.DEFAULT_CHUNK_SIZE
        self.cursor_name = cursor_name or f"fm_lazy_{id(self)}_{int(time.time() * 1000) % 10000}"

        self._cursor = None
        self._closed = False
        self._total_fetched = 0
        self._chunks_fetched = 0
        self._start_time = None

    def __enter__(self) -> 'LazyResultIterator':
        """Open the server-side cursor."""
        if not POSTGRESQL_AVAILABLE:
            raise RuntimeError("psycopg2 not available for lazy iteration")

        self._start_time = time.time()

        # Create NAMED cursor for server-side cursor behavior
        # Named cursors in psycopg2 use PostgreSQL server-side cursors
        self._cursor = self.connection.cursor(name=self.cursor_name)

        # Set cursor properties for optimal streaming
        self._cursor.itersize = self.chunk_size

        # Execute query
        self._cursor.execute(self.query, self.params)

        logger.debug(
            f"LazyResultIterator opened: {self.cursor_name} "
            f"(chunk_size: {self.chunk_size})"
        )

        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Close the cursor and log statistics."""
        self.close()
        return False

    def __iter__(self) -> Iterator[List[Any]]:
        """Iterate over result chunks."""
        while True:
            chunk = self._cursor.fetchmany(self.chunk_size)
            if not chunk:
                break

            self._chunks_fetched += 1
            self._total_fetched += len(chunk)

            # Extract first column (typically IDs) from each row
            yield [row[0] for row in chunk]

    def fetch_all_ids(self) -> List[Any]:
        """
        Fetch all IDs at once (use with caution for large results).

        Only use this for result sets you know are reasonably sized.
        For unknown sizes, iterate with __iter__ instead.
        """
        all_ids = []
        for chunk in self:
            all_ids.extend(chunk)
        return all_ids

    def close(self):
        """Close the cursor and log statistics."""
        if not self._closed and self._cursor:
            try:
                self._cursor.close()
            except Exception as e:
                logger.debug(f"Error closing lazy iterator cursor: {e}")

            self._closed = True

            elapsed_ms = (time.time() - self._start_time) * 1000 if self._start_time else 0

            logger.debug(
                f"LazyResultIterator closed: {self.cursor_name} "
                f"(total: {self._total_fetched:,} rows in {self._chunks_fetched} chunks, "
                f"time: {elapsed_ms:.1f}ms)"
            )

    @property
    def total_fetched(self) -> int:
        """Get total number of rows fetched so far."""
        return self._total_fetched

    @property
    def chunks_fetched(self) -> int:
        """Get number of chunks fetched so far."""
        return self._chunks_fetched


class TwoPhaseFilter:
    """
    Two-phase spatial filter for optimal performance on complex expressions.

    Phase 1 (Fast Pre-filter):
    - Uses ONLY bounding box intersection (&&) which leverages GIST index
    - Very fast, reduces candidate set significantly

    Phase 2 (Precise Filter):
    - Applies full spatial predicates only on Phase 1 candidates
    - Much faster because working on reduced dataset

    Performance Comparison (100k features, complex buffer + predicates):
    - Single-phase: ~15 seconds
    - Two-phase: ~2-4 seconds (3-7x faster)

    Usage:
        filter = TwoPhaseFilter(conn, layer_props)
        result = filter.execute(
            full_expression="ST_Intersects(...) AND ST_Within(...)",
            source_bbox=(xmin, ymin, xmax, ymax)
        )
    """

    # Minimum features to benefit from two-phase filtering
    MIN_FEATURES_THRESHOLD = 10000

    # Target reduction ratio in phase 1 to justify two-phase approach
    TARGET_REDUCTION_RATIO = 0.3  # Phase 1 should reduce to < 30% of original

    def __init__(
        self,
        connection,
        layer_props: Union[LayerProperties, Dict],
        chunk_size: int = 5000
    ):
        """
        Initialize two-phase filter.

        Args:
            connection: psycopg2 database connection
            layer_props: Layer properties (schema, table, geometry column, pk)
            chunk_size: Chunk size for result streaming
        """
        self.connection = connection
        self.chunk_size = chunk_size

        # Convert dict to LayerProperties if needed
        if isinstance(layer_props, dict):
            self.layer_props = LayerProperties.from_dict(layer_props)
        else:
            self.layer_props = layer_props

    def execute(
        self,
        full_expression: str,
        source_bbox: Optional[Tuple[float, float, float, float]] = None,
        source_geometry_wkt: Optional[str] = None,
        progress_callback: Optional[Callable[[int, int], None]] = None
    ) -> FilterResult:
        """
        Execute two-phase filtering.

        Args:
            full_expression: Complete SQL WHERE clause with spatial predicates
            source_bbox: Bounding box (xmin, ymin, xmax, ymax) for phase 1
            source_geometry_wkt: WKT of source geometry (alternative to bbox)
            progress_callback: Callback(current, total) for progress reporting

        Returns:
            FilterResult with feature IDs and execution statistics
        """
        start_time = time.time()

        try:
            # Determine if we can use two-phase approach
            if source_bbox is None and source_geometry_wkt is None:
                # No spatial bounds - fall back to single phase
                return self._execute_single_phase(full_expression, progress_callback)

            # Build bbox from WKT if not provided directly
            if source_bbox is None and source_geometry_wkt:
                source_bbox = self._extract_bbox_from_wkt(source_geometry_wkt)

            if source_bbox is None:
                return self._execute_single_phase(full_expression, progress_callback)

            # ===== PHASE 1: Fast Bounding Box Pre-filter =====
            phase1_start = time.time()

            candidate_ids = self._execute_phase1_bbox(source_bbox, progress_callback)

            phase1_time = (time.time() - phase1_start) * 1000
            candidates_count = len(candidate_ids)

            logger.info(
                f"📦 Phase 1 (bbox): {candidates_count:,} candidates "
                f"in {phase1_time:.1f}ms"
            )

            # Check if phase 1 was effective enough
            if candidates_count == 0:
                return FilterResult(
                    success=True,
                    feature_ids=[],
                    feature_count=0,
                    strategy_used=FilterStrategy.TWO_PHASE,
                    execution_time_ms=(time.time() - start_time) * 1000,
                    phases_executed=1,
                    phase1_time_ms=phase1_time,
                    candidates_after_phase1=0
                )

            # If very few candidates, skip phase 2 optimization
            if candidates_count <= 100:
                logger.debug("Few candidates after phase 1, applying full expression directly")
                return self._execute_phase2_on_candidates(
                    full_expression, candidate_ids, phase1_time, start_time, progress_callback
                )

            # ===== PHASE 2: Full Predicate on Candidates =====
            time.time()

            final_ids = self._execute_phase2_on_candidates(
                full_expression, candidate_ids, phase1_time, start_time, progress_callback
            )

            return final_ids

        except Exception as e:
            logger.error(f"Two-phase filter error: {e}")
            import traceback
            logger.debug(traceback.format_exc())

            return FilterResult(
                success=False,
                error=str(e),
                execution_time_ms=(time.time() - start_time) * 1000,
                strategy_used=FilterStrategy.TWO_PHASE
            )

    def _execute_phase1_bbox(
        self,
        bbox: Tuple[float, float, float, float],
        progress_callback: Optional[Callable] = None
    ) -> List[int]:
        """
        Execute Phase 1: Fast bounding box filter using GIST index.

        Uses the && operator which is optimized for GIST spatial indexes.
        """
        xmin, ymin, xmax, ymax = bbox

        # Build efficient bbox query using && operator
        query = """
            SELECT "{self.layer_props.primary_key}"
            FROM "{self.layer_props.schema}"."{self.layer_props.table}"
            WHERE "{self.layer_props.geometry_column}" &&
                  ST_MakeEnvelope({xmin}, {ymin}, {xmax}, {ymax}, {self.layer_props.srid})
        """

        # Use lazy iterator for memory efficiency
        candidate_ids = []

        with LazyResultIterator(
            self.connection, query, chunk_size=self.chunk_size
        ) as iterator:
            for chunk in iterator:
                candidate_ids.extend(chunk)

                if progress_callback:
                    # Report progress (phase 1)
                    progress_callback(len(candidate_ids), -1)  # -1 = unknown total

        return candidate_ids

    def _execute_phase2_on_candidates(
        self,
        full_expression: str,
        candidate_ids: List[int],
        phase1_time: float,
        overall_start_time: float,
        progress_callback: Optional[Callable] = None
    ) -> FilterResult:
        """
        Execute Phase 2: Apply full expression only on candidates from Phase 1.
        """
        phase2_start = time.time()

        # Build IN clause with candidate IDs
        # For very large candidate sets, chunk the query
        final_ids = []

        # Process in chunks to avoid PostgreSQL query length limits
        chunk_size = 10000  # Max IDs per IN clause

        for i in range(0, len(candidate_ids), chunk_size):
            chunk = candidate_ids[i:i + chunk_size]
            ','.join(str(id) for id in chunk)

            query = """
                SELECT "{self.layer_props.primary_key}"
                FROM "{self.layer_props.schema}"."{self.layer_props.table}"
                WHERE "{self.layer_props.primary_key}" IN ({ids_list})
                  AND ({full_expression})
            """

            try:
                with self.connection.cursor() as cursor:
                    cursor.execute(query)
                    rows = cursor.fetchall()
                    final_ids.extend([row[0] for row in rows])

            except Exception as e:
                logger.error(f"Phase 2 chunk error: {e}")
                # Continue with next chunk
                continue

            if progress_callback:
                progress_callback(len(final_ids), len(candidate_ids))

        phase2_time = (time.time() - phase2_start) * 1000
        total_time = (time.time() - overall_start_time) * 1000

        # Calculate reduction ratio
        reduction_ratio = 1.0 - (len(final_ids) / len(candidate_ids)) if candidate_ids else 0.0

        logger.info(
            f"✓ Phase 2 (full): {len(final_ids):,} results "
            f"(reduced {reduction_ratio:.1%}) in {phase2_time:.1f}ms"
        )

        # Estimate memory saved vs fetching all at once
        # Assume ~100 bytes per feature with geometry
        memory_saved_mb = (len(candidate_ids) * 100) / (1024 * 1024)

        return FilterResult(
            success=True,
            feature_ids=final_ids,
            feature_count=len(final_ids),
            strategy_used=FilterStrategy.TWO_PHASE,
            execution_time_ms=total_time,
            phases_executed=2,
            phase1_time_ms=phase1_time,
            phase2_time_ms=phase2_time,
            candidates_after_phase1=len(candidate_ids),
            reduction_ratio=reduction_ratio,
            memory_saved_estimate_mb=memory_saved_mb
        )

    def _execute_single_phase(
        self,
        expression: str,
        progress_callback: Optional[Callable] = None
    ) -> FilterResult:
        """Execute single-phase filter (fallback when two-phase not possible)."""
        start_time = time.time()

        query = """
            SELECT "{self.layer_props.primary_key}"
            FROM "{self.layer_props.schema}"."{self.layer_props.table}"
            WHERE {expression}
        """

        feature_ids = []

        with LazyResultIterator(
            self.connection, query, chunk_size=self.chunk_size
        ) as iterator:
            for chunk in iterator:
                feature_ids.extend(chunk)

                if progress_callback:
                    progress_callback(len(feature_ids), -1)

        return FilterResult(
            success=True,
            feature_ids=feature_ids,
            feature_count=len(feature_ids),
            strategy_used=FilterStrategy.DIRECT,
            execution_time_ms=(time.time() - start_time) * 1000,
            phases_executed=1
        )

    def _extract_bbox_from_wkt(self, wkt: str) -> Optional[Tuple[float, float, float, float]]:
        """
        Extract bounding box from WKT geometry.

        Uses a simple regex approach for common WKT formats.
        Falls back to database computation for complex geometries.
        """
        try:
            # Try to parse coordinates from WKT
            # Pattern matches POLYGON((x1 y1, x2 y2, ...))
            coord_pattern = r'[-+]?\d*\.?\d+\s+[-+]?\d*\.?\d+'
            matches = re.findall(coord_pattern, wkt)

            if not matches:
                return None

            coords = []
            for match in matches:
                parts = match.split()
                if len(parts) == 2:
                    coords.append((float(parts[0]), float(parts[1])))

            if not coords:
                return None

            xs = [c[0] for c in coords]
            ys = [c[1] for c in coords]

            return (min(xs), min(ys), max(xs), max(ys))

        except Exception as e:
            logger.debug(f"Could not parse bbox from WKT: {e}")

            # Fallback: use database to compute bbox
            # FIX v3.0.20 (CRIT-002): Use parameterized query to prevent SQL injection
            try:
                with self.connection.cursor() as cursor:
                    cursor.execute("SELECT ST_Extent(ST_GeomFromText(%s))", (wkt,))
                    result = cursor.fetchone()
                    if result and result[0]:
                        # Parse BOX(xmin ymin, xmax ymax) format
                        box_str = result[0]
                        box_match = re.match(
                            r'BOX\(([-\d.]+)\s+([-\d.]+),\s*([-\d.]+)\s+([-\d.]+)\)',
                            box_str
                        )
                        if box_match:
                            return tuple(float(x) for x in box_match.groups())
            except Exception:  # nosec B110 - best-effort parse, falls back below
                pass

            return None


class ProgressiveFilterExecutor:
    """
    Main executor for progressive filtering operations.

    Coordinates between different filtering strategies and provides
    a unified interface for the PostgreSQL backend.

    Strategies:
    1. DIRECT - Simple setSubsetString for small datasets
    2. MATERIALIZED - MV with GIST index for medium datasets
    3. TWO_PHASE - Bbox pre-filter for complex expressions
    4. PROGRESSIVE - Chunked streaming for very large results

    Usage:
        executor = ProgressiveFilterExecutor(conn, layer_props)
        result = executor.execute_optimal(
            expression=complex_expression,
            source_bounds=bbox,
            complexity_score=estimator.estimate(expression)
        )
    """

    # Strategy thresholds
    DIRECT_MAX_FEATURES = 10000
    MATERIALIZED_MAX_FEATURES = 100000
    TWO_PHASE_MIN_COMPLEXITY = 50

    def __init__(
        self,
        connection,
        layer_props: Union[LayerProperties, Dict],
        config: Optional[Dict] = None
    ):
        """
        Initialize progressive filter executor.

        Args:
            connection: psycopg2 database connection
            layer_props: Layer properties
            config: Optional configuration overrides
        """
        self.connection = connection

        if isinstance(layer_props, dict):
            self.layer_props = LayerProperties.from_dict(layer_props)
        else:
            self.layer_props = layer_props

        # Apply configuration
        self.config = config or {}
        self.chunk_size = self.config.get('chunk_size', 5000)
        self.enable_two_phase = self.config.get('enable_two_phase', True)
        self.enable_streaming = self.config.get('enable_streaming', True)

    def execute_optimal(
        self,
        expression: str,
        source_bounds: Optional[Tuple[float, float, float, float]] = None,
        source_wkt: Optional[str] = None,
        complexity_score: float = 0.0,
        force_strategy: Optional[FilterStrategy] = None,
        progress_callback: Optional[Callable[[int, int], None]] = None
    ) -> FilterResult:
        """
        Execute filter using optimal strategy based on parameters.

        Args:
            expression: SQL WHERE clause
            source_bounds: Bounding box for two-phase optimization
            source_wkt: Source geometry WKT (alternative to bounds)
            complexity_score: Query complexity score (from estimator)
            force_strategy: Force specific strategy (for testing)
            progress_callback: Progress reporting callback

        Returns:
            FilterResult with IDs and statistics
        """
        # Determine optimal strategy
        if force_strategy:
            strategy = force_strategy
        else:
            strategy = self._choose_strategy(
                expression, source_bounds, source_wkt, complexity_score
            )

        logger.info(f"📊 Using filter strategy: {strategy.value}")

        # Execute with chosen strategy
        if strategy == FilterStrategy.TWO_PHASE:
            two_phase = TwoPhaseFilter(
                self.connection, self.layer_props, self.chunk_size
            )
            return two_phase.execute(
                expression, source_bounds, source_wkt, progress_callback
            )

        elif strategy == FilterStrategy.PROGRESSIVE:
            return self._execute_progressive(expression, progress_callback)

        elif strategy == FilterStrategy.LAZY_CURSOR:
            return self._execute_lazy_cursor(expression, progress_callback)

        else:
            # DIRECT strategy
            return self._execute_direct(expression, progress_callback)

    def _choose_strategy(
        self,
        expression: str,
        source_bounds: Optional[Tuple],
        source_wkt: Optional[str],
        complexity_score: float
    ) -> FilterStrategy:
        """Choose optimal strategy based on query characteristics."""

        feature_count = self.layer_props.estimated_feature_count
        has_bounds = source_bounds is not None or source_wkt is not None

        # Very complex expressions benefit from two-phase
        if (self.enable_two_phase and
            has_bounds and
                complexity_score >= self.TWO_PHASE_MIN_COMPLEXITY):
            return FilterStrategy.TWO_PHASE

        # Large datasets should use streaming
        if feature_count > self.MATERIALIZED_MAX_FEATURES:
            if self.enable_streaming:
                return FilterStrategy.PROGRESSIVE
            return FilterStrategy.LAZY_CURSOR

        # Medium datasets with bounds can use two-phase
        if (self.enable_two_phase and
            has_bounds and
                feature_count > self.DIRECT_MAX_FEATURES):
            return FilterStrategy.TWO_PHASE

        # Default to direct for small datasets
        return FilterStrategy.DIRECT

    def _execute_progressive(
        self,
        expression: str,
        progress_callback: Optional[Callable] = None
    ) -> FilterResult:
        """Execute with progressive chunked result retrieval."""
        start_time = time.time()

        query = """
            SELECT "{self.layer_props.primary_key}"
            FROM "{self.layer_props.schema}"."{self.layer_props.table}"
            WHERE {expression}
        """

        feature_ids = []
        chunk_count = 0

        with LazyResultIterator(
            self.connection, query, chunk_size=self.chunk_size
        ) as iterator:
            for chunk in iterator:
                feature_ids.extend(chunk)
                chunk_count += 1

                if progress_callback:
                    progress_callback(len(feature_ids), -1)

                # Log progress for long-running queries
                if chunk_count % 20 == 0:
                    logger.debug(
                        f"Progressive filter: {len(feature_ids):,} IDs "
                        f"fetched in {chunk_count} chunks"
                    )

        return FilterResult(
            success=True,
            feature_ids=feature_ids,
            feature_count=len(feature_ids),
            strategy_used=FilterStrategy.PROGRESSIVE,
            execution_time_ms=(time.time() - start_time) * 1000,
            phases_executed=1
        )

    def _execute_lazy_cursor(
        self,
        expression: str,
        progress_callback: Optional[Callable] = None
    ) -> FilterResult:
        """Execute with lazy cursor (minimal memory footprint)."""
        # Same as progressive but with smaller chunk size
        original_chunk_size = self.chunk_size
        self.chunk_size = min(1000, self.chunk_size)  # Smaller chunks

        try:
            result = self._execute_progressive(expression, progress_callback)
            result.strategy_used = FilterStrategy.LAZY_CURSOR
            return result
        finally:
            self.chunk_size = original_chunk_size

    def _execute_direct(
        self,
        expression: str,
        progress_callback: Optional[Callable] = None
    ) -> FilterResult:
        """Execute with direct fetchall (for small datasets)."""
        start_time = time.time()

        query = """
            SELECT "{self.layer_props.primary_key}"
            FROM "{self.layer_props.schema}"."{self.layer_props.table}"
            WHERE {expression}
        """

        try:
            with self.connection.cursor() as cursor:
                cursor.execute(query)
                rows = cursor.fetchall()
                feature_ids = [row[0] for row in rows]

            if progress_callback:
                progress_callback(len(feature_ids), len(feature_ids))

            return FilterResult(
                success=True,
                feature_ids=feature_ids,
                feature_count=len(feature_ids),
                strategy_used=FilterStrategy.DIRECT,
                execution_time_ms=(time.time() - start_time) * 1000,
                phases_executed=1
            )

        except Exception as e:
            return FilterResult(
                success=False,
                error=str(e),
                execution_time_ms=(time.time() - start_time) * 1000,
                strategy_used=FilterStrategy.DIRECT
            )

    def build_chunked_in_expression(
        self,
        feature_ids: List[int],
        max_ids_per_clause: int = 10000
    ) -> Generator[str, None, None]:
        """
        Build IN clause expressions in chunks.

        For very large ID lists, PostgreSQL may have issues with
        extremely long IN clauses. This generator yields multiple
        smaller IN clauses.

        Usage:
            for in_clause in executor.build_chunked_in_expression(ids):
                layer.setSubsetString(in_clause)
                # Process chunk...
        """
        pk = self.layer_props.primary_key

        for i in range(0, len(feature_ids), max_ids_per_clause):
            chunk = feature_ids[i:i + max_ids_per_clause]
            ids_str = ','.join(str(id) for id in chunk)
            yield f'"{pk}" IN ({ids_str})'


# Convenience function for quick filtering
def progressive_filter(
    connection,
    layer_props: Dict,
    expression: str,
    source_bounds: Optional[Tuple[float, float, float, float]] = None,
    complexity_score: float = 0.0
) -> FilterResult:
    """
    Convenience function for progressive filtering.

    Usage:
        result = progressive_filter(
            conn,
            {'schema': 'public', 'table': 'buildings', 'primary_key': 'gid'},
            "ST_Intersects(geom, ST_GeomFromText('POLYGON(...)'))",
            source_bounds=(0, 0, 100, 100)
        )
    """
    executor = ProgressiveFilterExecutor(connection, layer_props)
    return executor.execute_optimal(
        expression,
        source_bounds=source_bounds,
        complexity_score=complexity_score
    )
