import { Router } from 'express';
import type { Request, Response } from 'express';
import { authenticate } from '../../middleware/auth.middleware';
import { suggestionsService } from './suggestions.service';
import { checkSocialEnabled } from './social.middleware';

const router = Router();
router.use(authenticate);
router.use(checkSocialEnabled);

/** GET /api/social/suggestions — ranked friend suggestions */
router.get('/', async (req: Request, res: Response) => {
  try {
    const limit = Math.min(Math.max(parseInt(req.query.limit as string) || 10, 1), 20);
    const suggestions = await suggestionsService.getSuggestions(req.user!.id, limit);
    res.json({ suggestions });
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'SUGGESTIONS_ERROR' } });
  }
});

/** POST /api/social/suggestions/:userId/dismiss — dismiss a suggestion */
router.post('/:userId/dismiss', async (req: Request, res: Response) => {
  try {
    const suggestedUserId = req.params.userId as string;
    const result = await suggestionsService.dismissSuggestion(req.user!.id, suggestedUserId);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'DISMISS_ERROR' } });
  }
});

export { router as suggestionsRouter };
