"""Goal service""" from typing import Optional, List from sqlalchemy.orm import Session from app.db.repositories import GoalRepository from app.db.models import Goal from app.schemas import GoalCreateSchema class GoalService: """Service for goal operations""" def __init__(self, session: Session): self.session = session self.goal_repo = GoalRepository(session) def create_goal(self, family_id: int, data: GoalCreateSchema) -> Goal: """Create new savings goal""" return self.goal_repo.create( family_id=family_id, name=data.name, description=data.description, target_amount=data.target_amount, priority=data.priority, target_date=data.target_date, account_id=data.account_id, ) def add_to_goal(self, goal_id: int, amount: float) -> Optional[Goal]: """Add amount to goal progress""" return self.goal_repo.update_progress(goal_id, amount) def get_goal_progress(self, goal_id: int) -> dict: """Get goal progress information""" goal = self.goal_repo.get_by_id(goal_id) if not goal: return {} progress_percent = (goal.current_amount / goal.target_amount * 100) if goal.target_amount > 0 else 0 return { "goal_id": goal.id, "name": goal.name, "target": goal.target_amount, "current": goal.current_amount, "remaining": goal.target_amount - goal.current_amount, "progress_percent": progress_percent, "is_completed": goal.is_completed, "target_date": goal.target_date, } def get_family_goals_progress(self, family_id: int) -> List[dict]: """Get progress for all family goals""" goals = self.goal_repo.get_family_goals(family_id) return [self.get_goal_progress(goal.id) for goal in goals] def complete_goal(self, goal_id: int) -> Optional[Goal]: """Mark goal as completed""" from datetime import datetime return self.goal_repo.update( goal_id, is_completed=True, completed_at=datetime.utcnow() )