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

from src.auth.schemas import PlanCurrentCompanyUser
from src.schemas.compnay_user import CurrentCompanyUser
from src.schemas.application_user import CurrentApplicationUser


oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/company/user/auth/login")  


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


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

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


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



