from fastapi import APIRouter, Depends, HTTPException, status from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy import select from app.dependencies import get_db, get_current_user from app.models.user import User from app.models.entry import Entry, EntryType from app.schemas.entry import EntryCreate, EntryResponse, BottleStats router = APIRouter(prefix="/api/entries", tags=["entries"]) def _calc_stats(entries: list[Entry]) -> BottleStats: adds = [e for e in entries if e.entry_type == EntryType.add] removes = [e for e in entries if e.entry_type == EntryType.remove] total_add_shots = sum(e.amount_shots for e in adds) total_remove_shots = sum(e.amount_shots for e in removes) current_total = total_add_shots - total_remove_shots # Weighted average proof across all add entries weighted_proof_sum = sum(e.proof * e.amount_shots for e in adds if e.proof is not None) proof_shot_total = sum(e.amount_shots for e in adds if e.proof is not None) estimated_proof = (weighted_proof_sum / proof_shot_total) if proof_shot_total > 0 else None return BottleStats( total_add_entries=len(adds), current_total_shots=round(current_total, 2), estimated_proof=round(estimated_proof, 1) if estimated_proof is not None else None, ) @router.get("", response_model=list[EntryResponse]) async def list_entries( db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): result = await db.execute( select(Entry) .where(Entry.user_id == current_user.id) .order_by(Entry.date.desc(), Entry.created_at.desc()) ) return result.scalars().all() @router.post("", response_model=EntryResponse, status_code=status.HTTP_201_CREATED) async def create_entry( body: EntryCreate, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): if body.entry_type == EntryType.add and not body.bourbon_name: raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail="bourbon_name is required for add entries") entry = Entry( user_id=current_user.id, entry_type=body.entry_type, date=body.date, bourbon_name=body.bourbon_name, proof=body.proof, amount_shots=body.amount_shots, notes=body.notes, ) async with db.begin(): db.add(entry) await db.refresh(entry) return entry @router.delete("/{entry_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_entry( entry_id: int, db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): result = await db.execute( select(Entry).where(Entry.id == entry_id, Entry.user_id == current_user.id) ) entry = result.scalar_one_or_none() if not entry: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Entry not found") async with db.begin(): await db.delete(entry) @router.get("/stats", response_model=BottleStats) async def get_stats( db: AsyncSession = Depends(get_db), current_user: User = Depends(get_current_user), ): result = await db.execute(select(Entry).where(Entry.user_id == current_user.id)) entries = result.scalars().all() return _calc_stats(entries)