import express from 'express';
import { OidcService } from './oidcService';

const router = express.Router();

// oidcCallback must be invoked lazily — OidcService.getInstance() is only
// set after server.ts calls OidcService.initialize() during startup.
// Calling it at module-load time (as a plain `const`) crashes the server.
const oidcCallback = (req: express.Request, res: express.Response, next: express.NextFunction) => {
	OidcService.callback()(req, res, next);
};

// /.well-known/openid-configuration
router.get('/.well-known/openid-configuration', oidcCallback);

// /oauth/* — all OIDC endpoints
router.get('/oauth/authorize', oidcCallback);
router.post('/oauth/authorize', oidcCallback);
router.post('/oauth/token', oidcCallback);
router.get('/oauth/userinfo', oidcCallback);
router.get('/oauth/jwks', oidcCallback);
router.post('/oauth/introspect', oidcCallback);
router.post('/oauth/revoke', oidcCallback);
router.get('/oauth/logout', oidcCallback);
router.post('/oauth/logout', oidcCallback);

export default router;
