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

export { generateSlug } from '../../utils/slug';

/** Generate a moderator JWT for a given room + user */
export function generateModeratorToken(
  user: { id: string; email: string; name: string | null },
  room: string,
): string {
  const payload = {
    context: {
      user: {
        name: user.name || user.email,
        email: user.email,
        id: user.id,
        moderator: true,
      },
    },
    aud: env.JITSI_APP_ID,
    iss: env.JITSI_APP_ID,
    sub: 'meet.jitsi',
    room,
  };

  return jwt.sign(payload, env.JITSI_APP_SECRET, {
    algorithm: 'HS256',
    expiresIn: '2h',
  });
}
