from jose import jwt
from datetime import datetime, timedelta
import os
from uuid import UUID
from utils.logging import logger

SECRET_KEY=os.getenv('SECRET_KEY',"")
ALGORITHM=os.getenv('ALGORITHM',"")

logger.info(f"Secret key :{SECRET_KEY} andAlgrothm: {ALGORITHM}")

def create_access_token(user_id:UUID):
    payload={
        "user_id":str(user_id),
        "exp":datetime.now()+timedelta(days=30)
    }

    token=jwt.encode(
        payload,
        SECRET_KEY,
        algorithm=ALGORITHM
    )

    return token

def decode_access_token(token:str):
    return jwt.decode(token,SECRET_KEY,algorithms=ALGORITHM)

def create_reset_password_token(user_id: UUID):
    payload={
        "user_id": str(user_id),
        "type": "reset_password",
        "exp": datetime.now() + timedelta(minutes=15)
    }
    
    token = jwt.encode(
        payload,
        SECRET_KEY,
        algorithm=ALGORITHM
    )
    return token

def decode_reset_password_token(token: str):
    return jwt.decode(token, SECRET_KEY, algorithms=ALGORITHM)