"""
Wizard API Endpoints - Handles initialization wizard steps
"""

import time
from typing import Optional

from fastapi import APIRouter, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel

from file_brain.core.logging import logger
from file_brain.database.models import db_session
from file_brain.database.repositories.wizard_state_repository import WizardStateRepository
from file_brain.services.docker_manager import get_docker_manager
from file_brain.services.startup_checker import get_startup_checker
from file_brain.services.typesense_client import get_typesense_client

router = APIRouter(prefix="/wizard", tags=["wizard"])


class WizardStatusResponse(BaseModel):
    """Response model for wizard status"""

    wizard_completed: bool
    docker_check_passed: bool
    docker_services_started: bool
    collection_created: bool
    last_step_completed: int
    current_step: int


class DockerCheckResponse(BaseModel):
    """Response model for docker check"""

    available: bool
    command: Optional[str] = None
    version: Optional[str] = None
    error: Optional[str] = None


class DockerStartResponse(BaseModel):
    """Response model for docker start"""

    success: bool
    message: Optional[str] = None
    error: Optional[str] = None


class DockerStatusResponse(BaseModel):
    """Response model for docker status"""

    success: bool
    running: bool
    healthy: bool
    services: list
    error: Optional[str] = None


class CollectionCreateResponse(BaseModel):
    """Response model for collection creation"""

    success: bool
    message: Optional[str] = None
    error: Optional[str] = None


class CollectionStatusResponse(BaseModel):
    """Response model for collection status"""

    exists: bool
    ready: bool
    document_count: Optional[int] = None
    error: Optional[str] = None


class CheckDetailResponse(BaseModel):
    """Response model for individual check detail"""

    passed: bool
    message: str


class StartupCheckResponse(BaseModel):
    """Response model for comprehensive startup checks"""

    all_checks_passed: bool
    needs_wizard: bool
    start_step: Optional[int] = None
    is_upgrade: bool
    checks: dict  # Maps check name to CheckDetailResponse


@router.get("/status", response_model=WizardStatusResponse)
async def get_wizard_status():
    """Get current wizard completion status"""
    try:
        with db_session() as db:
            repo = WizardStateRepository(db)
            state = repo.get_or_create()

            # Determine current step based on completion
            current_step = 0
            if not state.docker_check_passed:
                current_step = 0
            elif not state.docker_services_started:
                current_step = 1
            elif not state.collection_created:
                current_step = 2
            elif state.wizard_completed:
                current_step = 3
            else:
                current_step = state.last_step_completed

            return WizardStatusResponse(
                wizard_completed=state.wizard_completed,
                docker_check_passed=state.docker_check_passed,
                docker_services_started=state.docker_services_started,
                collection_created=state.collection_created,
                last_step_completed=state.last_step_completed,
                current_step=current_step,
            )
    except Exception as e:
        logger.error(f"Error getting wizard status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/startup-check", response_model=StartupCheckResponse)
async def check_startup_requirements():
    """
    Perform comprehensive startup checks to determine if wizard is needed.

    This endpoint validates all system requirements and returns detailed status
    for each check. Unlike the /status endpoint which reads from DB, this performs
    actual validation of external conditions (Docker, images, services, model, collection).

    Returns:
        Detailed status of each check, plus which wizard step to start from if any checks fail
    """
    try:
        from file_brain.core.telemetry import telemetry

        checker = get_startup_checker()
        result = await checker.perform_all_checks()

        # Track wizard start if needed
        if result.needs_wizard:
            telemetry.capture_event(
                "wizard_started",
                {
                    "start_step": result.get_first_failed_step(),
                    "is_upgrade": result.is_upgrade,
                    "docker_available": result.docker_available.passed,
                    "docker_images": result.docker_images.passed,
                    "services_healthy": result.services_healthy.passed,
                    "model_downloaded": result.model_downloaded.passed,
                    "collection_ready": result.collection_ready.passed,
                    "schema_current": result.schema_current.passed,
                },
            )

        return StartupCheckResponse(
            all_checks_passed=result.all_checks_passed,
            needs_wizard=result.needs_wizard,
            start_step=result.get_first_failed_step(),
            is_upgrade=result.is_upgrade,
            checks={
                "docker_available": {
                    "passed": result.docker_available.passed,
                    "message": result.docker_available.message,
                },
                "docker_images": {
                    "passed": result.docker_images.passed,
                    "message": result.docker_images.message,
                },
                "services_healthy": {
                    "passed": result.services_healthy.passed,
                    "message": result.services_healthy.message,
                },
                "model_downloaded": {
                    "passed": result.model_downloaded.passed,
                    "message": result.model_downloaded.message,
                },
                "collection_ready": {
                    "passed": result.collection_ready.passed,
                    "message": result.collection_ready.message,
                },
                "schema_current": {
                    "passed": result.schema_current.passed,
                    "message": result.schema_current.message,
                },
                "wizard_reset": {
                    "passed": result.wizard_reset.passed,
                    "message": result.wizard_reset.message,
                },
            },
        )
    except Exception as e:
        logger.error(f"Error performing startup checks: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/docker-check", response_model=DockerCheckResponse)
async def check_docker():
    """Check if Docker/Podman is installed"""
    try:
        from file_brain.core.telemetry import telemetry

        docker_manager = get_docker_manager()
        info = docker_manager.get_docker_info()

        # Update wizard state if docker is available
        if info.get("available"):
            with db_session() as db:
                repo = WizardStateRepository(db)
                repo.update_docker_check(True)

            # Track successful docker check
            telemetry.capture_event(
                "wizard_step_docker_check",
                {
                    "success": True,
                    "docker_command": info.get("command"),
                    "docker_version": info.get("version"),
                },
            )

        return DockerCheckResponse(
            available=info.get("available", False),
            command=info.get("command"),
            version=info.get("version"),
            error=info.get("error"),
        )
    except Exception as e:
        logger.error(f"Error checking docker: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/docker-images-check")
async def check_docker_images():
    """Check if required docker images are present locally"""
    try:
        docker_manager = get_docker_manager()
        return await docker_manager.check_required_images()
    except Exception as e:
        logger.error(f"Error checking docker images: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/docker-pull")
async def pull_docker_images():
    """Pull docker images with real progress updates via SSE"""
    import json
    from asyncio import Queue

    docker_manager = get_docker_manager()

    async def event_generator():
        """Generate SSE events from docker pull progress"""

        # Check if docker is available
        if not docker_manager.is_docker_available():
            yield "data: " + json.dumps({"error": "Docker/Podman not found"}) + "\n\n"
            return

        # Use a queue to collect progress events
        progress_queue: Queue = Queue()
        pull_complete = False
        pull_error = None

        async def progress_callback(data: dict):
            """Callback for each progress event"""
            await progress_queue.put(data)

        # Start pull in background task
        import asyncio

        async def do_pull():
            nonlocal pull_complete, pull_error
            try:
                from file_brain.core.telemetry import telemetry

                logger.info("Starting docker pull...")
                start_time = time.time()
                result = await docker_manager.pull_images_with_progress(progress_callback)
                duration = time.time() - start_time
                logger.info(f"Docker pull completed: {result}")

                if not result.get("success"):
                    pull_error = result.get("error")
                else:
                    # Track successful docker pull
                    telemetry.capture_event(
                        "wizard_step_docker_pull_completed",
                        {
                            "success": True,
                            "duration_seconds": round(duration, 2),
                        },
                    )

                pull_complete = True
                await progress_queue.put(None)  # Signal completion
            except Exception as e:
                logger.error(f"Docker pull error: {e}", exc_info=True)
                pull_error = str(e)
                pull_complete = True
                await progress_queue.put(None)

        asyncio.create_task(do_pull())

        # Stream progress events
        logger.info("Starting SSE stream...")
        while True:
            try:
                data = await asyncio.wait_for(progress_queue.get(), timeout=60.0)
                if data is None:  # Completion signal
                    if pull_error:
                        yield "data: " + json.dumps({"error": pull_error}) + "\n\n"
                    break
                yield "data: " + json.dumps({**data, "timestamp": time.time()}) + "\n\n"
            except asyncio.TimeoutError:
                yield "data: " + json.dumps({"heartbeat": True}) + "\n\n"
            except Exception as e:
                logger.error(f"Error streaming docker pull: {e}")
                yield "data: " + json.dumps({"error": str(e)}) + "\n\n"
                break

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
        },
    )


@router.post("/docker-start", response_model=DockerStartResponse)
async def start_docker_services():
    """Start docker-compose services"""
    try:
        from file_brain.core.telemetry import telemetry

        docker_manager = get_docker_manager()

        # Check if docker is available
        if not docker_manager.is_docker_available():
            raise HTTPException(
                status_code=400,
                detail="Docker/Podman not found. Please install Docker or Podman first.",
            )

        # Start services
        result = await docker_manager.start_services()

        # Update wizard state if successful
        if result.get("success"):
            with db_session() as db:
                repo = WizardStateRepository(db)
                repo.update_docker_services(True)
                repo.update_last_step(1)

            # Track successful docker start
            telemetry.capture_event(
                "wizard_step_docker_started",
                {"success": True},
            )

        return DockerStartResponse(
            success=result.get("success", False),
            message=result.get("message"),
            error=result.get("error"),
        )
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Error starting docker services: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/docker-status", response_model=DockerStatusResponse)
async def get_docker_status():
    """Get status of docker-compose services"""
    try:
        docker_manager = get_docker_manager()
        result = await docker_manager.get_services_status()

        # Update wizard state if services are running
        if result.get("running"):
            with db_session() as db:
                repo = WizardStateRepository(db)
                repo.update_docker_services(True)

        return DockerStatusResponse(
            success=result.get("success", False),
            running=result.get("running", False),
            healthy=result.get("healthy", False),
            services=result.get("services", []),
            error=result.get("error"),
        )
    except Exception as e:
        logger.error(f"Error getting docker status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/docker-logs")
async def stream_docker_logs():
    """Stream docker-compose logs via Server-Sent Events"""

    async def event_generator():
        """Generate SSE events from docker logs"""
        docker_manager = get_docker_manager()

        try:
            # Check if docker is available
            if not docker_manager.is_docker_available():
                yield "data: {'error': 'Docker/Podman not found'}\n\n"
                return

            async def log_callback(log_line: str):
                """Callback for each log line"""
                # Send as SSE event
                import json

                yield f"data: {json.dumps({'log': log_line, 'timestamp': time.time()})}\n\n"

            # Stream logs
            await docker_manager.stream_all_logs(log_callback)

        except Exception as e:
            logger.error(f"Error streaming docker logs: {e}")
            import json

            yield f"data: {json.dumps({'error': str(e)})}\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
        },
    )


class ModelStatusResponse(BaseModel):
    """Response model for model status"""

    exists: bool
    path: str
    files: list
    missing_files: list


@router.get("/model-status", response_model=ModelStatusResponse)
async def get_model_status():
    """Check if embedding model is already downloaded"""
    try:
        from file_brain.services.model_downloader import get_model_downloader

        downloader = get_model_downloader()
        status = downloader.check_model_exists()

        return ModelStatusResponse(
            exists=status["exists"],
            path=status["path"],
            files=status["files"],
            missing_files=status["missing_files"],
        )
    except Exception as e:
        logger.error(f"Error checking model status: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/model-download")
async def download_model():
    """Download embedding model with progress updates via SSE"""
    import json
    from asyncio import Queue

    from file_brain.services.model_downloader import get_model_downloader

    downloader = get_model_downloader()

    async def event_generator():
        """Generate SSE events from model download progress"""

        # First check if model already exists
        status = downloader.check_model_exists()
        if status["exists"]:
            yield (
                "data: "
                + json.dumps(
                    {
                        "status": "complete",
                        "message": "Model already downloaded",
                        "complete": True,
                        "progress_percent": 100,
                    }
                )
                + "\n\n"
            )
            return

        # Use a queue to collect progress events
        progress_queue: Queue = Queue()
        download_complete = False
        download_error = None

        async def progress_callback(data: dict):
            """Callback for each progress event"""
            await progress_queue.put(data)

        # Start download in background task
        import asyncio

        async def do_download():
            nonlocal download_complete, download_error
            try:
                from file_brain.core.telemetry import telemetry

                logger.info("Starting model download...")
                start_time = time.time()
                result = await downloader.download_model_with_progress(progress_callback)
                duration = time.time() - start_time
                logger.info(f"Model download completed: {result}")

                if not result.get("success"):
                    download_error = result.get("error")
                else:
                    # Track successful model download
                    telemetry.capture_event(
                        "wizard_step_model_download_completed",
                        {
                            "success": True,
                            "duration_seconds": round(duration, 2),
                        },
                    )

                download_complete = True
                await progress_queue.put(None)  # Signal completion
            except Exception as e:
                logger.error(f"Model download error: {e}", exc_info=True)
                download_error = str(e)
                download_complete = True
                await progress_queue.put(None)

        asyncio.create_task(do_download())

        # Stream progress events
        logger.info("Starting model download SSE stream...")
        while True:
            try:
                data = await asyncio.wait_for(progress_queue.get(), timeout=120.0)
                if data is None:  # Completion signal
                    if download_error:
                        yield "data: " + json.dumps({"error": download_error}) + "\n\n"
                    break
                yield "data: " + json.dumps({**data, "timestamp": time.time()}) + "\n\n"
            except asyncio.TimeoutError:
                yield "data: " + json.dumps({"heartbeat": True}) + "\n\n"
            except Exception as e:
                logger.error(f"Error streaming model download: {e}")
                yield "data: " + json.dumps({"error": str(e)}) + "\n\n"
                break

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
        },
    )


@router.post("/collection-create", response_model=CollectionCreateResponse)
async def create_collection():
    """Create Typesense collection (non-blocking - runs in background)"""
    import asyncio

    async def _create_collection_task():
        """Background task to create collection"""
        from file_brain.core.telemetry import telemetry
        from file_brain.services.service_manager import get_service_manager

        # Reset service state and clear logs for fresh start
        service_manager = get_service_manager()
        service_manager.reset_service_for_retry("typesense")

        try:
            typesense = get_typesense_client()

            # Initialize collection
            await typesense.initialize_collection()

            # Check if collection is ready
            if typesense.collection_ready:
                # Update wizard state
                with db_session() as db:
                    repo = WizardStateRepository(db)
                    repo.update_collection_created(True)
                    repo.update_last_step(2)
                logger.info("Collection creation completed successfully")

                # Track successful collection creation
                telemetry.capture_event(
                    "wizard_step_collection_created",
                    {"success": True},
                )
            else:
                logger.error("Collection creation failed")

        except Exception as e:
            logger.error(f"Error creating collection: {e}", exc_info=True)
            telemetry.capture_exception(e)

    # Start the background task
    asyncio.create_task(_create_collection_task())

    # Return immediately
    return CollectionCreateResponse(
        success=True,
        message="Collection creation started in background",
    )


@router.get("/collection-status", response_model=CollectionStatusResponse)
async def get_collection_status():
    """Get status of Typesense collection"""
    try:
        typesense = get_typesense_client()

        # Check if collection exists
        # We explicitly check against Typesense instead of relying on the local flag
        ready = await typesense.check_collection_exists()

        # Get document count if available
        doc_count = None
        if ready:
            try:
                result = await typesense.get_stats()
                doc_count = result.get("totals", {}).get("indexed", 0)
            except Exception:
                pass

        return CollectionStatusResponse(
            exists=ready,
            ready=ready,
            document_count=doc_count,
        )
    except Exception as e:
        logger.error(f"Error getting collection status: {e}")
        return CollectionStatusResponse(
            exists=False,
            ready=False,
            error=str(e),
        )


@router.post("/restart-typesense")
async def restart_typesense():
    """Restart Typesense container with fresh volume to recover from errors"""
    import asyncio
    import shutil

    from file_brain.core.paths import app_paths

    try:
        docker_manager = get_docker_manager()

        # Check if docker is available
        if not docker_manager.is_docker_available():
            raise HTTPException(
                status_code=400,
                detail="Docker/Podman not found",
            )

        # Build commands
        stop_cmd = [docker_manager.docker_cmd, "compose", "-f", str(docker_manager.compose_file), "stop", "typesense"]
        rm_cmd = [
            docker_manager.docker_cmd,
            "compose",
            "-f",
            str(docker_manager.compose_file),
            "rm",
            "-f",
            "-v",
            "typesense",
        ]
        start_cmd = [
            docker_manager.docker_cmd,
            "compose",
            "-f",
            str(docker_manager.compose_file),
            "up",
            "-d",
            "typesense",
        ]

        # Stop container first
        logger.info("Stopping Typesense...")
        proc = await asyncio.create_subprocess_exec(
            *stop_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
        )
        await proc.communicate()

        # Remove container
        logger.info("Removing Typesense container...")
        proc = await asyncio.create_subprocess_exec(
            *rm_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
        )
        await proc.communicate()

        # Clear the bind-mounted data directory (except models)
        # Since we now use a bind mount, we need to clear the host directory
        typesense_data_dir = app_paths.typesense_data_dir
        models_dir = app_paths.models_dir

        logger.info(f"Clearing Typesense data directory: {typesense_data_dir}")

        # Remove all contents except the models directory
        if typesense_data_dir.exists():
            for item in typesense_data_dir.iterdir():
                if item != models_dir:
                    try:
                        if item.is_dir():
                            shutil.rmtree(item)
                        else:
                            item.unlink()
                        logger.info(f"Removed: {item}")
                    except Exception as e:
                        logger.warning(f"Failed to remove {item}: {e}")

        logger.info("Starting fresh Typesense...")
        proc = await asyncio.create_subprocess_exec(
            *start_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
        )
        stdout, stderr = await proc.communicate()

        if proc.returncode != 0:
            error_msg = stderr.decode().strip()
            logger.error(f"Failed to restart Typesense: {error_msg}")
            return {"success": False, "error": error_msg}

        logger.info("Typesense restarted successfully with cleared data")
        return {"success": True, "message": "Typesense restarted with cleared data"}

    except Exception as e:
        logger.error(f"Error restarting Typesense: {e}", exc_info=True)
        raise HTTPException(status_code=500, detail=str(e))


@router.get("/collection-logs")
async def stream_collection_logs():
    """Stream Typesense Docker container logs via SSE"""
    import asyncio
    import json

    async def event_generator():
        """Generate SSE events from Typesense container logs"""
        docker_manager = get_docker_manager()

        try:
            # Check if docker is available
            if not docker_manager.is_docker_available():
                yield f"data: {json.dumps({'error': 'Docker/Podman not found'})}\n\n"
                return

            # Build logs command for typesense service
            logs_cmd = [
                docker_manager.docker_cmd,
                "compose",
                "-f",
                str(docker_manager.compose_file),
                "logs",
                "-f",
                "--tail=50",
                "typesense",
            ]

            # Start streaming logs
            proc = await asyncio.create_subprocess_exec(
                *logs_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.STDOUT
            )

            # Stream output line by line
            while True:
                line = await proc.stdout.readline()
                if not line:
                    break

                log_line = line.decode().strip()
                if log_line:
                    yield f"data: {json.dumps({'log': log_line, 'timestamp': time.time()})}\n\n"

        except asyncio.CancelledError:
            logger.info("Collection logs stream cancelled")
            if proc:
                proc.terminate()
                await proc.wait()
            raise
        except Exception as e:
            logger.error(f"Error streaming collection logs: {e}")
            yield f"data: {json.dumps({'error': str(e)})}\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )


@router.post("/complete")
async def complete_wizard():
    """Mark wizard as complete"""
    try:
        from file_brain.core.telemetry import telemetry

        with db_session() as db:
            repo = WizardStateRepository(db)
            state = repo.get_or_create()
            repo.mark_completed()

            # Track wizard completion
            telemetry.capture_event(
                "wizard_completed",
                {
                    "total_steps": state.last_step_completed + 1,
                    "docker_check_passed": state.docker_check_passed,
                    "docker_services_started": state.docker_services_started,
                    "collection_created": state.collection_created,
                },
            )

        return {
            "success": True,
            "message": "Wizard completed successfully",
            "timestamp": int(time.time() * 1000),
        }
    except Exception as e:
        logger.error(f"Error completing wizard: {e}")
        raise HTTPException(status_code=500, detail=str(e))


@router.post("/reset")
async def reset_wizard():
    """Reset wizard state (for testing/debugging)"""
    try:
        with db_session() as db:
            repo = WizardStateRepository(db)
            repo.reset()

        return {
            "success": True,
            "message": "Wizard state reset successfully",
            "timestamp": int(time.time() * 1000),
        }
    except Exception as e:
        logger.error(f"Error resetting wizard: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ============================================================================
# App Container Startup Endpoints (Post-Wizard)
# ============================================================================


@router.post("/app-containers-start")
async def start_app_containers():
    """
    Start Docker containers for the main app (after wizard is completed).
    This is called when the app loads to start containers in the background.
    Returns immediately while containers start asynchronously.
    """
    import asyncio

    async def _start_containers_task():
        """Background task to start containers"""
        try:
            docker_manager = get_docker_manager()

            if not docker_manager.is_docker_available():
                logger.error("Docker/Podman not available for app container startup")
                return

            logger.info("Starting app containers in background...")
            result = await docker_manager.start_services()

            if result.get("success"):
                logger.info("✅ App containers started successfully")
            else:
                logger.error(f"❌ Failed to start app containers: {result.get('error')}")

        except Exception as e:
            logger.error(f"Error in app container startup task: {e}", exc_info=True)

    # Start the background task
    asyncio.create_task(_start_containers_task())

    # Return immediately
    return {
        "success": True,
        "message": "Container startup initiated in background",
        "timestamp": int(time.time() * 1000),
    }


@router.get("/app-containers-status")
async def stream_app_containers_status():
    """
    Stream container startup status via SSE.
    Provides real-time updates on container health until all are ready.
    """
    import asyncio
    import json

    async def event_generator():
        """Generate SSE events for container status"""
        docker_manager = get_docker_manager()

        try:
            # Check if docker is available
            if not docker_manager.is_docker_available():
                yield f"data: {json.dumps({'error': 'Docker/Podman not found'})}\n\n"
                return

            # Poll container status until all healthy or timeout
            max_checks = 60  # 60 checks * 2 seconds = 2 minutes max
            check_count = 0

            while check_count < max_checks:
                try:
                    status_result = await docker_manager.get_services_status()

                    # Send status update
                    status_data = {
                        "success": status_result.get("success", False),
                        "running": status_result.get("running", False),
                        "healthy": status_result.get("healthy", False),
                        "services": status_result.get("services", []),
                        "timestamp": time.time(),
                    }
                    yield f"data: {json.dumps(status_data)}\n\n"

                    # If all healthy, we're done
                    if status_result.get("healthy"):
                        logger.info("All containers healthy - stopping status stream")
                        break

                    # Wait before next check
                    await asyncio.sleep(2)
                    check_count += 1

                except Exception as e:
                    logger.error(f"Error checking container status: {e}")
                    yield f"data: {json.dumps({'error': str(e)})}\n\n"
                    break

            # Send final completion event
            if check_count >= max_checks:
                timeout_data = {"timeout": True, "message": "Container startup timed out after 2 minutes"}
                yield f"data: {json.dumps(timeout_data)}\n\n"

        except Exception as e:
            logger.error(f"Error in container status stream: {e}")
            yield f"data: {json.dumps({'error': str(e)})}\n\n"

    return StreamingResponse(
        event_generator(),
        media_type="text/event-stream",
        headers={
            "Cache-Control": "no-cache",
            "Connection": "keep-alive",
            "X-Accel-Buffering": "no",
        },
    )
