import { Router } from 'express';
import { authenticate } from '../../middleware/auth.middleware';
import { requireRole } from '../../middleware/rbac.middleware';
import { SOCIAL_ROLES } from '../../utils/roles';
import { friendshipRouter } from './friendship.routes';
import { blockRouter } from './block.routes';
import { privacyRouter } from './privacy.routes';
import { notificationRouter } from './notification.routes';
import { profileRouter } from './profile.routes';
import { feedRouter } from './feed.routes';
import { suggestionsRouter } from './suggestions.routes';
import { pokeRouter } from './poke.routes';
import { recommendationRouter } from './recommendation.routes';
import { integrationRouter } from './integration.routes';
import { groupRouter } from './group.routes';
import { achievementsRouter } from './achievements.routes';
import { sseRouter } from './sse.routes';
import { socialAdminRouter } from './social-admin.routes';
import { referralRouter } from './referral.routes';
import { impactStoriesRouter } from './impact-stories.routes';
import { spotlightRouter } from './spotlight.routes';
import { challengeRouter } from './challenge.routes';

const router = Router();

// EventSource (SSE) doesn't support custom headers — accept token via query param
// Scoped to /sse path only to limit token-in-URL exposure to where it's truly needed
router.use((req, _res, next) => {
  if (req.query.token && !req.headers.authorization && req.path.startsWith('/sse')) {
    req.headers.authorization = `Bearer ${req.query.token}`;
  }
  next();
});

// All social routes require authentication
router.use(authenticate);

// Admin sub-router (requires admin role)
router.use('/admin', requireRole(...SOCIAL_ROLES), socialAdminRouter);

// Sub-routers
router.use('/friends', friendshipRouter);
router.use('/blocks', blockRouter);
router.use('/privacy', privacyRouter);
router.use('/notifications', notificationRouter);
router.use('/profile', profileRouter);
router.use('/feed', feedRouter);
router.use('/suggestions', suggestionsRouter);
router.use('/pokes', pokeRouter);
router.use('/recommendations', recommendationRouter);
router.use('/integration', integrationRouter);
router.use('/groups', groupRouter);
router.use('/achievements', achievementsRouter);
router.use('/sse', sseRouter);
router.use('/referrals', referralRouter);
router.use('/stories', impactStoriesRouter);
router.use('/spotlight', spotlightRouter);
router.use('/challenges', challengeRouter);

export { router as socialRouter };
