import { Router } from 'express';
import type { Request, Response } from 'express';
import { checkSocialEnabled } from './social.middleware';
import { integrationService } from './integration.service';

const router = Router();

router.use(checkSocialEnabled);

/** GET /api/social/integration/shifts/:shiftId/friends */
router.get('/shifts/:shiftId/friends', async (req: Request, res: Response) => {
  try {
    const userId = req.user!.id;
    const shiftId = req.params.shiftId as string;
    const result = await integrationService.getFriendsAttendingShift(userId, shiftId);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message } });
  }
});

/** GET /api/social/integration/campaigns/:campaignId/friends */
router.get('/campaigns/:campaignId/friends', async (req: Request, res: Response) => {
  try {
    const userId = req.user!.id;
    const campaignId = req.params.campaignId as string;
    const result = await integrationService.getFriendsInCampaign(userId, campaignId);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message } });
  }
});

/** GET /api/social/integration/straw-polls/:pollId/friends */
router.get('/straw-polls/:pollId/friends', async (req: Request, res: Response) => {
  try {
    const userId = req.user!.id;
    const pollId = req.params.pollId as string;
    const result = await integrationService.getFriendsInStrawPoll(userId, pollId);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message } });
  }
});

/** GET /api/social/integration/map/friends */
router.get('/map/friends', async (req: Request, res: Response) => {
  try {
    const userId = req.user!.id;
    const result = await integrationService.getFriendsActiveOnMap(userId);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message } });
  }
});

export { router as integrationRouter };
