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

interface AccessPayload {
  sub: string; // user id
}

export function signAccessToken(userId: string): string {
  return jwt.sign({ sub: userId } satisfies AccessPayload, env.jwtSecret, {
    expiresIn: ACCESS_TOKEN_TTL,
  });
}

export function verifyAccessToken(token: string): string | null {
  try {
    const decoded = jwt.verify(token, env.jwtSecret) as AccessPayload;
    return decoded.sub || null;
  } catch {
    return null;
  }
}
