"""
Middleware to clean up completed research records.
Runs in request context where we have database access.
"""

import random

from flask import session
from loguru import logger
from sqlalchemy.exc import OperationalError, PendingRollbackError, TimeoutError

from ...database.models import UserActiveResearch
from ...database.session_context import get_g_db_session
from .middleware_optimizer import should_skip_database_middleware


def cleanup_completed_research():
    """
    Clean up completed research records for the current user.
    Called as a before_request handler.

    Runs on only ~1% of requests to avoid unnecessary DB queries on
    every authenticated request (same sampling pattern used by
    session cleanup in middleware_optimizer.should_skip_session_cleanup).
    """
    # Skip for requests that don't need database access
    if should_skip_database_middleware():
        return

    # Only run cleanup on ~1% of requests to reduce per-request overhead
    if random.randint(1, 100) > 1:  # noqa: S311 — not cryptographic, just sampling
        return

    username = session.get("username")
    if not username:
        return

    db_session = get_g_db_session()
    if db_session:
        try:
            # Find completed researches that haven't been cleaned up
            from ..routes.globals import is_research_active

            # Get all active records for this user with limit and better error handling
            active_records = (
                db_session.query(UserActiveResearch)
                .filter_by(username=username)
                .limit(50)  # Limit to prevent excessive memory usage
                .all()
            )

            cleaned_count = 0
            for record in active_records:
                # Check if this research is still active
                if not is_research_active(record.research_id):
                    # Research has completed, clean up the record
                    db_session.delete(record)
                    cleaned_count += 1
                    logger.debug(
                        f"Cleaned up completed research {record.research_id} "
                        f"for user {username}"
                    )

            if cleaned_count > 0:
                db_session.commit()
                logger.info(
                    f"Cleaned up {cleaned_count} completed research records "
                    f"for user {username}"
                )

        except (OperationalError, PendingRollbackError, TimeoutError) as e:
            # Handle connection pool exhaustion and timeout errors gracefully
            logger.warning(
                f"Database connection issue during cleanup - skipping: {type(e).__name__}"
            )
            try:
                db_session.rollback()
            except Exception:
                # Even rollback might fail if connections are exhausted
                logger.warning(
                    "Could not rollback database session during cleanup error"
                )
        except Exception:
            # Don't let cleanup errors break the request
            logger.exception("Error cleaning up completed research")
            try:
                db_session.rollback()
            except Exception:
                logger.warning(
                    "Could not rollback database session during cleanup error"
                )
