from fastapi_users import models
from fastapi import FastAPI
from fastapi_users.db import MongoDBUserDatabase
import motor.motor_asyncio
from fastapi_users.authentication import CookieAuthentication
from fastapi_users import FastAPIUsers


class User(models.BaseModel):
    pass


class UserCreate(User, models.BaseUserCreate):
    pass


class UserUpdate(User, models.BaseUserUpdate):
    pass


class UserDB(User, models.BaseUserDB):
    pass


# DATABASE_URL = 'mongodb://root:root@localhost:27017'
DATABASE_URL = 'mongodb://127.0.0.1:27017/fastapi'
client = motor.motor_asyncio.AsyncIOMotorClient(DATABASE_URL)
db = client['database_name']
collection = db['users']

print(db, end='\n')
print(collection, end='\n')

SECRET = 'SECRET'

auth_backends = []

cookie_authentication = CookieAuthentication(
    secret=SECRET, lifetime_seconds=3600
)

auth_backends.append(cookie_authentication)

user_db = MongoDBUserDatabase(UserDB, collection)

fastapi_users = FastAPIUsers(
    user_db, auth_backends, User, UserCreate, UserUpdate, UserDB, SECRET
)

app = FastAPI()

app.include_router(fastapi_users.router, prefix='/users', tags=['users'])


# def on_after_register(user: User, request):
#     print(f'user {user} has been registered.')
