use crate::AuthResult;
use bon::Builder;
use jsonwebtoken::{
    decode, encode, get_current_timestamp, Algorithm, DecodingKey, EncodingKey, Header, TokenData,
    Validation,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct UserClaims {
    uid: String,
    exp: usize,
    claims: Option<Value>,
}

#[derive(Builder, Debug)]
pub struct JWT {
    secret: String,
    algorithm: Algorithm,
}

impl JWT {
    pub fn generate_token(
        &self,
        uid: String,
        expiration: u64,
        claims: Option<Value>,
    ) -> AuthResult<String> {
        let exp = (get_current_timestamp() + expiration) as usize;

        let claims = UserClaims { uid, exp, claims };

        let token = encode(
            &Header::new(self.algorithm),
            &claims,
            &EncodingKey::from_base64_secret(&self.secret)?,
        )?;

        Ok(token)
    }

    pub fn validate(&self, token: &str) -> AuthResult<TokenData<UserClaims>> {
        let mut validate = Validation::new(self.algorithm);
        validate.leeway = 0;

        Ok(decode::<UserClaims>(
            token,
            &DecodingKey::from_base64_secret(&self.secret)?,
            &validate,
        )?)
    }
}
