"""Coordination workflows 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_workflow import CoordWorkflowCreate, CoordWorkflowResponse, CoordWorkflowUpdate
from api.schemas.coord_work_item import CoordWorkItemResponse
from api.services import coord_workflow_service, coord_work_item_service

router = APIRouter()


@router.get("", response_model=dict, status_code=status.HTTP_200_OK)
def list_workflows(
    project_key: str | None = Query(default=None),
    status_filter: 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 workflows with optional filters."""
    workflows, total = coord_workflow_service.get_workflows(
        db, project_key=project_key, status_filter=status_filter, skip=skip, limit=limit
    )
    return {
        "total": total,
        "skip": skip,
        "limit": limit,
        "workflows": [CoordWorkflowResponse.model_validate(w) for w in workflows],
    }


@router.post("", response_model=CoordWorkflowResponse, status_code=status.HTTP_201_CREATED)
def create_workflow(
    data: CoordWorkflowCreate,
    db: Session = Depends(get_db),
):
    """Create a new coordination workflow."""
    workflow = coord_workflow_service.create_workflow(db, data)
    return CoordWorkflowResponse.model_validate(workflow)


@router.get("/{workflow_id}", response_model=dict, status_code=status.HTTP_200_OK)
def get_workflow(
    workflow_id: UUID,
    db: Session = Depends(get_db),
):
    """Get a workflow by ID including its work items."""
    workflow = coord_workflow_service.get_workflow_by_id(db, workflow_id)
    items, _ = coord_work_item_service.get_work_items(db, workflow_id=str(workflow_id))
    return {
        "workflow": CoordWorkflowResponse.model_validate(workflow),
        "work_items": [CoordWorkItemResponse.model_validate(i) for i in items],
    }


@router.put("/{workflow_id}", response_model=CoordWorkflowResponse, status_code=status.HTTP_200_OK)
def update_workflow(
    workflow_id: UUID,
    data: CoordWorkflowUpdate,
    db: Session = Depends(get_db),
):
    """Update a workflow."""
    workflow = coord_workflow_service.update_workflow(db, workflow_id, data)
    return CoordWorkflowResponse.model_validate(workflow)


@router.delete("/{workflow_id}", response_model=dict, status_code=status.HTTP_200_OK)
def delete_workflow(
    workflow_id: UUID,
    db: Session = Depends(get_db),
):
    """Delete a workflow and its work items (cascade)."""
    return coord_workflow_service.delete_workflow(db, workflow_id)
