from fastapi.security import OAuth2PasswordBearer
from fastapi import Depends, HTTPException, status,Request
from jose import JWTError, jwt
from .schemas import CurrentCompnayUserModel,CurrentApplicationUserModel

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="user-login")  


SECRET_KEY = "thrinath@123"  
ALGORITHM = "HS256"

def get_current_user(token: str = Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        # Extract user details
        print("----------------------",payload)
        return CurrentCompnayUserModel(**payload) 
    except JWTError:
        print(JWTError)
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid authentication credentials....",
            headers={"WWW-Authenticate": "Bearer"},
        )
    

def get_current_application_user(token: str = Depends(oauth2_scheme)):
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        # Extract user details
        return CurrentApplicationUserModel(**payload) 
    except JWTError:
        print(JWTError)
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid authentication credentials",
            headers={"WWW-Authenticate": "Bearer"},
        )



