import uuid
from fastapi_users import FastAPIUsers, models
from fastapi_users.authentication import (
    AuthenticationBackend,
    BearerTransport,
    JWTStrategy,
)
from fastapi_users.jwt import SecretType

from src.database.db import Database
from src.database.user import User

class Transport:
    def __init__(self, secret: SecretType, db: Database):
        self.secret = secret
        self.db = db
        self.bearer_transport = BearerTransport(tokenUrl="auth/jwt/login")
        self.auth_backend = AuthenticationBackend(
            name="jwt",
            transport=self.bearer_transport,
            get_strategy=self.get_jwt_strategy,
        )
        self.fastapi_users = FastAPIUsers[User, uuid.UUID](self.db.get_user_manager, [self.auth_backend])
        self.current_active_user = self.fastapi_users.current_user(active=True)

    def get_jwt_strategy(self) -> JWTStrategy[models.UP, models.ID]:
        return JWTStrategy(secret=self.secret, lifetime_seconds=3600)
