import jwt from 'jsonwebtoken'
import { env } from '../config/env'

export interface TokenPayload {
  userId: number
  role: string
  email: string
}

// Sign a short-lived access token.
export const signAccessToken = (payload: TokenPayload): string =>
  jwt.sign(payload, env.jwtSecret, { expiresIn: env.jwtExpiresIn } as jwt.SignOptions)

// Sign a longer-lived refresh token.
export const signRefreshToken = (payload: TokenPayload): string =>
  jwt.sign(payload, env.jwtRefreshSecret, { expiresIn: env.jwtRefreshExpiresIn } as jwt.SignOptions)

// Verify an access token.
export const verifyAccessToken = (token: string): TokenPayload =>
  jwt.verify(token, env.jwtSecret) as TokenPayload

// Verify a refresh token.
export const verifyRefreshToken = (token: string): TokenPayload =>
  jwt.verify(token, env.jwtRefreshSecret) as TokenPayload
