import motor.motor_asyncio
from fastapi import FastAPI
from fastapi_users import FastAPIUsers, models
# from fastapi_users.authentication import CookieAuthentication
from fastapi_users.authentication import JWTAuthentication
from fastapi_users.db import MongoDBUserDatabase
from starlette.requests import Request
from starlette.templating import Jinja2Templates
from starlette.routing import Route


templates = Jinja2Templates(directory='templates')


DATABASE_URL = 'mongodb://localhost:27017'
SECRET = 'SECRET'


class User(models.BaseUser):
    pass


class UserCreate(User, models.BaseUserCreate):
    pass


class UserUpdate(User, models.BaseUserUpdate):
    pass


class UserDB(User, models.BaseUserDB):
    pass


client = motor.motor_asyncio.AsyncIOMotorClient(DATABASE_URL)
db = client['database_name']
collection = db['users']
user_db = MongoDBUserDatabase(UserDB, collection)

auth_backends = [
    JWTAuthentication(secret=SECRET, lifetime_seconds=3600),
    # CookieAuthentication(secret=SECRET, lifetime_seconds=3600),
]

app = FastAPI()
fastapi_users = FastAPIUsers(
    user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET,
)
app.include_router(fastapi_users.router, prefix='/users', tags=['users'])


@fastapi_users.on_after_register()
def on_after_register(user: User, request: Request):
    print(f'User {user.id} has registered with cookie.')


@fastapi_users.on_after_forgot_password()
def on_after_forgot_password(user: User, token: str, request: Request):
    print(f'User {user.id} has forgot their password. Reset token: {token}')


# app.include_router(Route('/', endpoint=home))
