""" DecisionLog API router for ClaudeTools. Defines all REST API endpoints for managing decision logs, tracking important decisions made during work. """ from uuid import UUID from fastapi import APIRouter, Depends, HTTPException, Query, status from sqlalchemy.orm import Session from api.database import get_db from api.middleware.auth import get_current_user from api.schemas.decision_log import ( DecisionLogCreate, DecisionLogResponse, DecisionLogUpdate, ) from api.services import decision_log_service # Create router with prefix and tags router = APIRouter() @router.get( "", response_model=dict, summary="List all decision logs", description="Retrieve a paginated list of all decision logs", status_code=status.HTTP_200_OK, ) def list_decision_logs( skip: int = Query( default=0, ge=0, description="Number of records to skip for pagination" ), limit: int = Query( default=100, ge=1, le=1000, description="Maximum number of records to return (max 1000)" ), db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ List all decision logs with pagination. Returns decision logs ordered by most recent first. """ try: logs, total = decision_log_service.get_decision_logs(db, skip, limit) return { "total": total, "skip": skip, "limit": limit, "logs": [DecisionLogResponse.model_validate(log) for log in logs] } except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to retrieve decision logs: {str(e)}" ) @router.get( "/by-impact/{impact}", response_model=dict, summary="Get decision logs by impact level", description="Retrieve decision logs filtered by impact level", status_code=status.HTTP_200_OK, ) def get_decision_logs_by_impact( impact: str, skip: int = Query(default=0, ge=0), limit: int = Query(default=100, ge=1, le=1000), db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Get decision logs filtered by impact level. Valid impact levels: low, medium, high, critical """ try: logs, total = decision_log_service.get_decision_logs_by_impact( db, impact, skip, limit ) return { "total": total, "skip": skip, "limit": limit, "impact": impact, "logs": [DecisionLogResponse.model_validate(log) for log in logs] } except HTTPException: raise except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to retrieve decision logs: {str(e)}" ) @router.get( "/by-project/{project_id}", response_model=dict, summary="Get decision logs by project", description="Retrieve all decision logs for a specific project", status_code=status.HTTP_200_OK, ) def get_decision_logs_by_project( project_id: UUID, skip: int = Query(default=0, ge=0), limit: int = Query(default=100, ge=1, le=1000), db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Get all decision logs for a specific project. """ try: logs, total = decision_log_service.get_decision_logs_by_project( db, project_id, skip, limit ) return { "total": total, "skip": skip, "limit": limit, "project_id": str(project_id), "logs": [DecisionLogResponse.model_validate(log) for log in logs] } except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to retrieve decision logs: {str(e)}" ) @router.get( "/by-session/{session_id}", response_model=dict, summary="Get decision logs by session", description="Retrieve all decision logs for a specific session", status_code=status.HTTP_200_OK, ) def get_decision_logs_by_session( session_id: UUID, skip: int = Query(default=0, ge=0), limit: int = Query(default=100, ge=1, le=1000), db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Get all decision logs for a specific session. """ try: logs, total = decision_log_service.get_decision_logs_by_session( db, session_id, skip, limit ) return { "total": total, "skip": skip, "limit": limit, "session_id": str(session_id), "logs": [DecisionLogResponse.model_validate(log) for log in logs] } except Exception as e: raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to retrieve decision logs: {str(e)}" ) @router.get( "/{log_id}", response_model=DecisionLogResponse, summary="Get decision log by ID", description="Retrieve a single decision log by its unique identifier", status_code=status.HTTP_200_OK, ) def get_decision_log( log_id: UUID, db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Get a specific decision log by ID. """ log = decision_log_service.get_decision_log_by_id(db, log_id) return DecisionLogResponse.model_validate(log) @router.post( "", response_model=DecisionLogResponse, summary="Create new decision log", description="Create a new decision log with the provided details", status_code=status.HTTP_201_CREATED, ) def create_decision_log( log_data: DecisionLogCreate, db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Create a new decision log. Requires a valid JWT token with appropriate permissions. """ log = decision_log_service.create_decision_log(db, log_data) return DecisionLogResponse.model_validate(log) @router.put( "/{log_id}", response_model=DecisionLogResponse, summary="Update decision log", description="Update an existing decision log's details", status_code=status.HTTP_200_OK, ) def update_decision_log( log_id: UUID, log_data: DecisionLogUpdate, db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Update an existing decision log. Only provided fields will be updated. All fields are optional. """ log = decision_log_service.update_decision_log(db, log_id, log_data) return DecisionLogResponse.model_validate(log) @router.delete( "/{log_id}", response_model=dict, summary="Delete decision log", description="Delete a decision log by its ID", status_code=status.HTTP_200_OK, ) def delete_decision_log( log_id: UUID, db: Session = Depends(get_db), current_user: dict = Depends(get_current_user), ): """ Delete a decision log. This is a permanent operation and cannot be undone. """ return decision_log_service.delete_decision_log(db, log_id)