"""Coordination work items 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_work_item import CoordWorkItemCreate, CoordWorkItemResponse, CoordWorkItemUpdate
from api.services import coord_work_item_service

router = APIRouter()


@router.get("", response_model=dict, status_code=status.HTTP_200_OK)
def list_work_items(
    workflow_id: str | None = Query(default=None),
    project_key: str | None = Query(default=None),
    status_filter: str | None = Query(default=None),
    assigned_session: str | None = Query(default=None),
    skip: int = Query(default=0, ge=0),
    limit: int = Query(default=100, ge=1, le=1000),
    db: Session = Depends(get_db),
):
    """List work items with optional filters."""
    items, total = coord_work_item_service.get_work_items(
        db,
        workflow_id=workflow_id,
        project_key=project_key,
        status_filter=status_filter,
        assigned_session=assigned_session,
        skip=skip,
        limit=limit,
    )
    return {
        "total": total,
        "skip": skip,
        "limit": limit,
        "work_items": [CoordWorkItemResponse.model_validate(i) for i in items],
    }


@router.post("", response_model=CoordWorkItemResponse, status_code=status.HTTP_201_CREATED)
def create_work_item(
    data: CoordWorkItemCreate,
    db: Session = Depends(get_db),
):
    """Create a new work item within a workflow."""
    item = coord_work_item_service.create_work_item(db, data)
    return CoordWorkItemResponse.model_validate(item)


@router.get("/{item_id}", response_model=CoordWorkItemResponse, status_code=status.HTTP_200_OK)
def get_work_item(
    item_id: UUID,
    db: Session = Depends(get_db),
):
    """Get a work item by ID."""
    item = coord_work_item_service.get_work_item_by_id(db, item_id)
    return CoordWorkItemResponse.model_validate(item)


@router.put("/{item_id}", response_model=CoordWorkItemResponse, status_code=status.HTTP_200_OK)
def update_work_item(
    item_id: UUID,
    data: CoordWorkItemUpdate,
    db: Session = Depends(get_db),
):
    """Update a work item."""
    item = coord_work_item_service.update_work_item(db, item_id, data)
    return CoordWorkItemResponse.model_validate(item)


@router.delete("/{item_id}", response_model=dict, status_code=status.HTTP_200_OK)
def delete_work_item(
    item_id: UUID,
    db: Session = Depends(get_db),
):
    """Delete a work item."""
    return coord_work_item_service.delete_work_item(db, item_id)
