import { Router } from 'express';
import type { Request, Response } from 'express';
import { authenticate } from '../../middleware/auth.middleware';
import { privacyService } from './privacy.service';
import { checkSocialEnabled } from './social.middleware';
import { privacyUpdateSchema } from './social.schemas';

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

/** GET /api/social/privacy — get my privacy settings */
router.get('/', async (req: Request, res: Response) => {
  try {
    const settings = await privacyService.getSettings(req.user!.id);
    res.json(settings);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'PRIVACY_GET_ERROR' } });
  }
});

/** PUT /api/social/privacy — update my privacy settings */
router.put('/', async (req: Request, res: Response) => {
  try {
    const data = privacyUpdateSchema.parse(req.body);
    const settings = await privacyService.updateSettings(req.user!.id, data);
    res.json(settings);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'PRIVACY_UPDATE_ERROR' } });
  }
});

export { router as privacyRouter };
