from fastapi import Depends, HTTPException

from sqlalchemy.orm import Session

class UsersRoutes:

    def __init__(self, bbs_app, crud, router, schemas):

        self.bbs_app = bbs_app

        @router.post("/", response_model=schemas.User)
        def create_user(user: schemas.UserCreate,
                        db: Session = Depends(self.bbs_app.get_db)):

            db_user = \
                crud.get_user_by_username(db, username=user.username)
            if db_user:
                raise HTTPException(status_code=400,
                                    detail="Username already registered")
            return crud.create_user(db=db, user=user)

        @router.get("/", response_model=list[schemas.User])
        def read_users(skip: int = 0, limit: int = 100,
                       db: Session = Depends(self.bbs_app.get_db)):
            users = crud.get_users(db, skip=skip, limit=limit)
            return users

        @router.get("/{user_id}", response_model=schemas.User)
        def read_user(user_id: int,
                      db: Session = Depends(self.bbs_app.get_db)):
            db_user = crud.get_user(db, user_id=user_id)
            if db_user is None:
                raise HTTPException(status_code=404, detail="User not found")
            return db_user
