from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from contextlib import asynccontextmanager from backend.database import engine, Base, SessionLocal from backend.models import User, ChoreType, ChoreCompletion, UserRole, RecurrenceType from backend.routers import auth, users, chores, reports from backend.config import get_current_week_identifier from datetime import datetime, timezone, timedelta def seed_initial_data(): Base.metadata.create_all(bind=engine) db = SessionLocal() try: # Seed Users if empty if db.query(User).count() == 0: admin_user = User( email="admin@choreus.app", name="Sarah (Admin)", avatar_url="https://api.dicebear.com/7.x/bottts/svg?seed=SarahAdmin", role=UserRole.ADMIN.value, weekly_star_quota=20 ) kid_user1 = User( email="leo@choreus.app", name="Leo", avatar_url="https://api.dicebear.com/7.x/bottts/svg?seed=LeoKid", role=UserRole.REGULAR.value, weekly_star_quota=15 ) kid_user2 = User( email="maya@choreus.app", name="Maya", avatar_url="https://api.dicebear.com/7.x/bottts/svg?seed=MayaKid", role=UserRole.REGULAR.value, weekly_star_quota=15 ) db.add_all([admin_user, kid_user1, kid_user2]) db.commit() # Seed Chores if empty if db.query(ChoreType).count() == 0: chores_list = [ ChoreType(title="Empty & fill dishwasher", description="Clear dishes, load dirty ones and start cycle", star_reward=1, recurrence=RecurrenceType.DAILY.value, icon="Utensils"), ChoreType(title="Take out trash & recycling", description="Empty kitchen bin and carry bins to curb", star_reward=1, recurrence=RecurrenceType.DAILY.value, icon="Trash2"), ChoreType(title="Vacuum living room", description="Vacuum carpet and couch cushions thoroughly", star_reward=3, recurrence=RecurrenceType.WEEKLY.value, icon="Broom"), ChoreType(title="Clean bathroom", description="Scrub sink, mirror, toilet, and wipe floor", star_reward=4, recurrence=RecurrenceType.WEEKLY.value, icon="Sparkles"), ChoreType(title="Clear snow from driveway", description="Shovel front driveway and salt walkways after snow fall", star_reward=5, recurrence=RecurrenceType.SPONTANEOUS.value, icon="Snowflake"), ChoreType(title="Go buy groceries", description="Pick up weekly groceries from list", star_reward=4, recurrence=RecurrenceType.SPONTANEOUS.value, icon="ShoppingBag"), ] db.add_all(chores_list) db.commit() # Seed sample completions if none exist if db.query(ChoreCompletion).count() == 0: users_list = db.query(User).all() chores_types = db.query(ChoreType).all() current_week = get_current_week_identifier() if len(users_list) >= 3 and len(chores_types) >= 3: c1 = ChoreCompletion( chore_type_id=chores_types[0].id, user_id=users_list[1].id, # Leo stars_earned=chores_types[0].star_reward, week_identifier=current_week, completed_at=datetime.now(timezone.utc) - timedelta(hours=5), notes="All clean!" ) c2 = ChoreCompletion( chore_type_id=chores_types[2].id, user_id=users_list[2].id, # Maya stars_earned=chores_types[2].star_reward, week_identifier=current_week, completed_at=datetime.now(timezone.utc) - timedelta(hours=2), notes="Living room carpet looks great" ) db.add_all([c1, c2]) db.commit() finally: db.close() @asynccontextmanager async def lifespan(app: FastAPI): seed_initial_data() yield app = FastAPI(title="ChoreUS API", version="1.0.0", description="Home Chore Manager API", lifespan=lifespan) app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) app.include_router(auth.router) app.include_router(users.router) app.include_router(chores.router) app.include_router(reports.router) @app.get("/") def root(): return {"message": "Welcome to ChoreUS Home Chore Manager API", "status": "running"}