"""Coordination inter-session messages router."""

from uuid import UUID

from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session

from api.database import get_db
from api.schemas.coord_message import CoordMessageCreate, CoordMessageResponse
from api.services import coord_message_service

router = APIRouter()


@router.get("", response_model=dict, status_code=status.HTTP_200_OK)
def list_messages(
    to_session: str | None = Query(default=None),
    unread_only: bool = Query(default=False),
    skip: int = Query(default=0, ge=0),
    limit: int = Query(default=100, ge=1, le=1000),
    db: Session = Depends(get_db),
):
    """List messages with optional filters."""
    messages, total = coord_message_service.get_messages(
        db, to_session=to_session, unread_only=unread_only, skip=skip, limit=limit
    )
    return {
        "total": total,
        "skip": skip,
        "limit": limit,
        "messages": [CoordMessageResponse.model_validate(m) for m in messages],
    }


@router.get("/unread-count", response_model=dict, status_code=status.HTTP_200_OK)
def get_unread_count(
    session_id: str = Query(...),
    db: Session = Depends(get_db),
):
    """Return the count of unread messages for a session."""
    count = coord_message_service.get_unread_count(db, session_id)
    return {"count": count}


@router.post("", response_model=CoordMessageResponse, status_code=status.HTTP_201_CREATED)
def send_message(
    data: CoordMessageCreate,
    db: Session = Depends(get_db),
):
    """Send a message to a session or broadcast."""
    msg = coord_message_service.send_message(db, data)
    return CoordMessageResponse.model_validate(msg)


@router.put("/{message_id}/read", response_model=CoordMessageResponse, status_code=status.HTTP_200_OK)
def mark_message_read(
    message_id: UUID,
    db: Session = Depends(get_db),
):
    """Mark a message as read."""
    msg = coord_message_service.mark_read(db, message_id)
    return CoordMessageResponse.model_validate(msg)


@router.delete("/{message_id}", response_model=dict, status_code=status.HTTP_200_OK)
def delete_message(
    message_id: UUID,
    db: Session = Depends(get_db),
):
    """Delete a message."""
    return coord_message_service.delete_message(db, message_id)
