"""Project management service.""" import logging from datetime import datetime, timezone from sqlalchemy import func, select from fabledassistant.models import async_session from fabledassistant.models.note import Note from fabledassistant.models.project import Project logger = logging.getLogger(__name__) async def create_project( user_id: int, title: str, description: str = "", goal: str = "", color: str | None = None, ) -> Project: async with async_session() as session: project = Project( user_id=user_id, title=title, description=description, goal=goal, color=color, status="active", ) session.add(project) await session.commit() await session.refresh(project) return project async def get_project(user_id: int, project_id: int) -> Project | None: async with async_session() as session: result = await session.execute( select(Project).where(Project.id == project_id, Project.user_id == user_id) ) return result.scalars().first() async def get_project_by_title(user_id: int, title: str) -> Project | None: async with async_session() as session: result = await session.execute( select(Project).where( Project.user_id == user_id, func.lower(Project.title) == func.lower(title.strip()), ).limit(1) ) return result.scalars().first() async def get_or_create_project(user_id: int, title: str) -> Project: project = await get_project_by_title(user_id, title) if project: return project return await create_project(user_id, title=title) async def list_projects(user_id: int, status: str | None = None) -> list[Project]: async with async_session() as session: query = select(Project).where(Project.user_id == user_id) if status: query = query.where(Project.status == status) query = query.order_by(Project.updated_at.desc()) result = await session.execute(query) return list(result.scalars().all()) async def update_project(user_id: int, project_id: int, **fields: object) -> Project | None: async with async_session() as session: result = await session.execute( select(Project).where(Project.id == project_id, Project.user_id == user_id) ) project = result.scalars().first() if project is None: return None for key, value in fields.items(): if hasattr(project, key): setattr(project, key, value) project.updated_at = datetime.now(timezone.utc) await session.commit() await session.refresh(project) return project async def delete_project(user_id: int, project_id: int) -> bool: async with async_session() as session: result = await session.execute( select(Project).where(Project.id == project_id, Project.user_id == user_id) ) project = result.scalars().first() if project is None: return False # Unlink notes (cascade handled by DB ON DELETE SET NULL, but we delete project here) await session.delete(project) await session.commit() return True async def get_project_summary(user_id: int, project_id: int) -> dict: """Return task counts by status, note count, and last activity.""" async with async_session() as session: # Task counts by status task_rows = await session.execute( select(Note.status, func.count(Note.id)) .where( Note.user_id == user_id, Note.project_id == project_id, Note.status.isnot(None), ) .group_by(Note.status) ) task_counts: dict[str, int] = {} for status, count in task_rows.fetchall(): task_counts[status] = count # Note count (non-tasks) note_count_result = await session.scalar( select(func.count(Note.id)).where( Note.user_id == user_id, Note.project_id == project_id, Note.status.is_(None), ) ) note_count = note_count_result or 0 # Last activity last_activity_result = await session.scalar( select(func.max(Note.updated_at)).where( Note.user_id == user_id, Note.project_id == project_id, ) ) from fabledassistant.services.milestones import get_project_milestone_summary milestone_summary = await get_project_milestone_summary(user_id, project_id) return { "task_counts": task_counts, "note_count": note_count, "last_activity": last_activity_result.isoformat() if last_activity_result else None, "milestone_summary": milestone_summary, }