"""User repository (global scope — users are not tenant-bound)."""

from __future__ import annotations

import uuid

from sqlalchemy import select
from sqlalchemy.orm import Session

from legate.db.models.user import User


def get_by_id(db: Session, user_id: uuid.UUID) -> User | None:
    return db.get(User, user_id)


def get_by_email(db: Session, email: str) -> User | None:
    return db.scalars(select(User).where(User.email == email.lower())).first()


def create(
    db: Session,
    *,
    email: str,
    password_hash: str | None,
    name: str = "",
    is_active: bool = True,
) -> User:
    user = User(
        email=email.lower(),
        password_hash=password_hash,
        name=name,
        is_active=is_active,
    )
    db.add(user)
    db.flush()
    return user
