"""Create the initial admin user if it doesn't already exist.""" import asyncio from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.config import settings from app.core.security import hash_password from app.database import async_session_factory from app.models.user import User async def seed_admin() -> None: async with async_session_factory() as db: result = await db.execute(select(User).where(User.email == settings.FIRST_ADMIN_EMAIL)) existing = result.scalar_one_or_none() if existing: print(f"Admin user already exists: {existing.email}") return admin = User( email=settings.FIRST_ADMIN_EMAIL, username=settings.FIRST_ADMIN_USERNAME, hashed_password=hash_password(settings.FIRST_ADMIN_PASSWORD), full_name="Administrator", role="admin", ) db.add(admin) await db.commit() print(f"Admin user created: {admin.email}") if __name__ == "__main__": asyncio.run(seed_admin())