"""Database layer: SQLAlchemy models, Pydantic schemas, repository, init."""

from duty_teller.db.models import Base, User, Duty, Role
from duty_teller.db.schemas import (
    UserCreate,
    UserInDb,
    DutyCreate,
    DutyInDb,
    DutyWithUser,
)
from duty_teller.db.session import (
    get_engine,
    get_session_factory,
    get_session,
    session_scope,
)
from duty_teller.db.repository import (
    delete_duties_in_range,
    get_or_create_user,
    get_or_create_user_by_full_name,
    get_duties,
    insert_duty,
    set_user_phone,
    update_user_display_name,
)

__all__ = [
    "Base",
    "User",
    "Duty",
    "Role",
    "UserCreate",
    "UserInDb",
    "DutyCreate",
    "DutyInDb",
    "DutyWithUser",
    "get_engine",
    "get_session_factory",
    "get_session",
    "session_scope",
    "delete_duties_in_range",
    "get_or_create_user",
    "get_or_create_user_by_full_name",
    "get_duties",
    "insert_duty",
    "set_user_phone",
    "update_user_display_name",
    "init_db",
]


def init_db(database_url: str) -> None:
    """Create all tables from SQLAlchemy metadata.

    Prefer Alembic migrations for schema changes in production.

    Args:
        database_url: SQLAlchemy database URL.
    """
    engine = get_engine(database_url)
    Base.metadata.create_all(bind=engine)
