import jwt, { SignOptions } from "jsonwebtoken";
import { env } from "./env.js";

export interface JwtPayload {
  id: number;
  email: string;
}

export class JwtAdapter {
  static generate(payload: JwtPayload): string {
    const options: SignOptions = {
      expiresIn: env.JWT_EXPIRES_IN as NonNullable<SignOptions["expiresIn"]>,
    };

    return jwt.sign(payload, env.JWT_SEED, options);
  }

  static validate(token: string): JwtPayload | null {
    try {
      return jwt.verify(token, env.JWT_SEED) as JwtPayload;
    } catch {
      return null;
    }
  }
}