from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from joserfc import jwt
from joserfc.errors import JoseError
from joserfc.jwk import OctKey
from datetime import datetime, timedelta
from app.models import User
from app.models.token_data import TokenData
from app.serializers import user_serialize
import pymongo
import app.config as config

# Database setup
client = pymongo.MongoClient(config.MONGODB_URL, username=config.MONGODB_USERNAME, password=config.MONGODB_PASSWORD)
db = client[config.MONGODB_DATABASE]

users_collection = db["users"]

oauth2_scheme = OAuth2PasswordBearer(tokenUrl=config.TOKEN_URL)

credentials_exception = HTTPException(
    status_code=status.HTTP_401_UNAUTHORIZED,
    detail="Could not validate credentials",
    headers={"WWW-Authenticate": "Bearer"},
)

async def get_current_user(token: str = Depends(oauth2_scheme)) -> User:
    try:
        payload = jwt.decode(token, OctKey.import_key(config.SECRET_KEY))
        username: str = payload.claims["sub"]
        expire_date = payload.claims["exp"]
        if username is None or int(datetime.now().timestamp()) > expire_date:
            raise credentials_exception
        token_data = TokenData(username=username)
    except:
        raise credentials_exception
    
    user = users_collection.find_one({"username": token_data.username})
    if user is None:
        raise credentials_exception
    
    return user_serialize(user)

def get_current_user_from_refresh_token(refresh_token: str) -> User:
    try:
        payload = jwt.decode(refresh_token, OctKey.import_key(config.SECRET_KEY))
        username: str = payload.claims["sub"]
        expire_date = payload.claims["exp"]
        if username is None or int(datetime.now().timestamp()) > expire_date:
            raise credentials_exception
    except:
        raise credentials_exception
    
    user = users_collection.find_one({"username": username})
    if user is None:
        raise credentials_exception
    
    return user

async def get_admin_user(current_user: User = Depends(get_current_user)):
    if not current_user.is_admin:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="You don't have admin privileges"
        )
    return current_user

def create_access_token(data: dict, expires_delta: timedelta):
    to_encode = data.copy()

    expire = datetime.now().timestamp() + expires_delta.total_seconds()

    to_encode.update({"exp": expire})
    header = {"alg": config.ALGORITHM}
    encoded_jwt = jwt.encode(header, to_encode, OctKey.import_key(config.SECRET_KEY))

    return encoded_jwt

def create_refresh_token(data: dict, expires_delta: timedelta):
    to_encode = data.copy()

    expire = datetime.now().timestamp() + expires_delta.total_seconds()

    to_encode.update({"exp": expire, "type": "refresh"})
    header = {"alg": config.ALGORITHM}
    encoded_jwt = jwt.encode(header, to_encode, OctKey.import_key(config.SECRET_KEY))
    
    return encoded_jwt

# Exceptions
def friend_not_found():
    raise HTTPException(
        status_code=status.HTTP_404_NOT_FOUND,
        detail="Friend not found"
    )

def objectid_misformatted():
    raise HTTPException(
        status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
        detail="The ObjectID is misformatted"
    )