
from fastapi import Depends, APIRouter
from sqlalchemy.orm import Session
from app.db import models
from app.core.security import get_current_user


# бд
def get_db():
    db = models.SessionLocal()
    try:
        yield db
    finally:
        db.close()

usersRouter = APIRouter(
    prefix="/users",
    tags=[],
)

# Пример защищенного роута
@usersRouter.get("/me")
async def read_users_me(current_user: models.User = Depends(get_current_user)):
    return {"id": current_user.id, "username": current_user.username}

@usersRouter.get("/all")
async def read_users_all(current_user: models.User = Depends(get_current_user), db: Session = Depends(get_db)):
    users = db.query(models.User).all()
    return [{"id": user.id, "username": user.username, "name": f"{user.first_name} {user.last_name or ''}".strip()} for user in users]