import type { Handler } from 'hono';
import { blacklistToken, cleanupExpiredTokens, clearVaultTokens } from 'officerdb';

export const signoutHandler: Handler = async (ctx) => {
  const user = ctx.get('user') as { id: number; jti: string; exp: number };

  await blacklistToken(user.jti, user.exp);

  // Drop the brokered vault session on logout (the protector key stays, so re-login is frictionless).
  clearVaultTokens(user.id).catch(() => {});

  // Opportunistic cleanup of expired tokens (non-blocking)
  cleanupExpiredTokens().catch(() => {});

  return ctx.json({ ok: true });
};
