"""WebSocket API endpoint for real-time updates."""

from fastapi import APIRouter, WebSocket, WebSocketDisconnect, Depends
from sqlalchemy.orm import Session
import logging
import json

from app.websocket import manager
from app.database import get_db
from app.crud import get_statistics

router = APIRouter(tags=["websocket"])
logger = logging.getLogger(__name__)


@router.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    """
    WebSocket endpoint for real-time event updates.

    Clients connect to this endpoint to receive:
    - New events as they're created
    - Updated statistics
    - Reset notifications

    Message types:
    - event_created: New event with updated statistics
    - stats_updated: Statistics update
    - stats_reset: All stats have been reset
    - connection_status: Connection status message
    """
    await manager.connect(websocket)

    try:
        # Send initial connection message
        await manager.send_personal_message(
            {"type": "connection_status", "message": "Connected to Claude Code Monitor"},
            websocket,
        )

        # Keep connection alive and handle incoming messages
        while True:
            try:
                # Receive messages from client (for future features like pings)
                data = await websocket.receive_text()
                message = json.loads(data)

                # Handle ping/pong for connection health
                if message.get("type") == "ping":
                    await manager.send_personal_message(
                        {"type": "pong", "timestamp": message.get("timestamp")},
                        websocket,
                    )

            except WebSocketDisconnect:
                logger.info("Client disconnected")
                break
            except Exception as e:
                logger.error(f"Error handling WebSocket message: {e}")
                # Don't break on error, continue listening

    except Exception as e:
        logger.error(f"WebSocket error: {e}")
    finally:
        manager.disconnect(websocket)
