import { Router } from 'express';
import type { Request, Response } from 'express';
import { authenticate } from '../../middleware/auth.middleware';
import { feedService } from './feed.service';
import { checkSocialEnabled } from './social.middleware';
import { friendsPaginationSchema } from './social.schemas';

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

/** GET /api/social/feed — paginated friends activity feed */
router.get('/', async (req: Request, res: Response) => {
  try {
    const { page, limit } = friendsPaginationSchema.parse(req.query);
    const result = await feedService.getFriendFeed(req.user!.id, page, limit);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'FEED_ERROR' } });
  }
});

/** GET /api/social/feed/my — own activity */
router.get('/my', async (req: Request, res: Response) => {
  try {
    const { page, limit } = friendsPaginationSchema.parse(req.query);
    const result = await feedService.getMyActivity(req.user!.id, page, limit);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'MY_ACTIVITY_ERROR' } });
  }
});

export { router as feedRouter };
