import uuid

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from domains.users.models import User


async def get_user_by_id(db: AsyncSession, user_id: str | uuid.UUID):
    result = await db.execute(
        select(User).where(User.id == user_id)
    )
    return result.scalar_one_or_none()

async def get_user_by_username(db: AsyncSession, username: str):
    result = await db.execute(
        select(User).where(User.username == username)
    )
    return result.scalar_one_or_none()

async def create_user(db: AsyncSession, user: User):
    db.add(user)
    await db.commit()
    await db.refresh(user)
    return user

async def get_all_users(db: AsyncSession, include_admin: bool = False):
    query = select(User)
    if not include_admin:
        query = query.where(User.is_admin.is_(False))

    result = await db.execute(query)
    return result.scalars().all()
