import { Router } from 'express';
import type { Request, Response } from 'express';
import { authenticate } from '../../middleware/auth.middleware';
import { notificationService } from './notification.service';
import { checkSocialEnabled } from './social.middleware';
import { notificationsPaginationSchema, notificationPreferencesUpdateSchema } from './social.schemas';

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

/** GET /api/social/notifications — list notifications */
router.get('/', async (req: Request, res: Response) => {
  try {
    const { page, limit, unreadOnly } = notificationsPaginationSchema.parse(req.query);
    const result = await notificationService.listNotifications(req.user!.id, page, limit, unreadOnly);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'NOTIFICATIONS_LIST_ERROR' } });
  }
});

/** GET /api/social/notifications/count — unread count */
router.get('/count', async (req: Request, res: Response) => {
  try {
    const count = await notificationService.getUnreadCount(req.user!.id);
    res.json({ count });
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'NOTIFICATION_COUNT_ERROR' } });
  }
});

/** GET /api/social/notifications/preferences — get preferences */
router.get('/preferences', async (req: Request, res: Response) => {
  try {
    const prefs = await notificationService.getPreferences(req.user!.id);
    res.json(prefs);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'NOTIFICATION_PREFS_GET_ERROR' } });
  }
});

/** PUT /api/social/notifications/preferences — update preferences */
router.put('/preferences', async (req: Request, res: Response) => {
  try {
    const data = notificationPreferencesUpdateSchema.parse(req.body);
    const prefs = await notificationService.updatePreferences(req.user!.id, data);
    res.json(prefs);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'NOTIFICATION_PREFS_UPDATE_ERROR' } });
  }
});

/** POST /api/social/notifications/:id/read — mark one as read */
router.post('/:id/read', async (req: Request, res: Response) => {
  try {
    const notificationId = parseInt(req.params.id as string, 10);
    if (isNaN(notificationId)) {
      res.status(400).json({ error: { message: 'Invalid notification ID', code: 'INVALID_ID' } });
      return;
    }
    const notification = await notificationService.markAsRead(req.user!.id, notificationId);
    res.json(notification);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'MARK_READ_ERROR' } });
  }
});

/** POST /api/social/notifications/read-all — mark all as read */
router.post('/read-all', async (req: Request, res: Response) => {
  try {
    const result = await notificationService.markAllAsRead(req.user!.id);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'MARK_ALL_READ_ERROR' } });
  }
});

/** DELETE /api/social/notifications/:id — delete */
router.delete('/:id', async (req: Request, res: Response) => {
  try {
    const notificationId = parseInt(req.params.id as string, 10);
    if (isNaN(notificationId)) {
      res.status(400).json({ error: { message: 'Invalid notification ID', code: 'INVALID_ID' } });
      return;
    }
    const result = await notificationService.deleteNotification(req.user!.id, notificationId);
    res.json(result);
  } catch (err: any) {
    res.status(err.statusCode || 500).json({ error: { message: err.message, code: 'DELETE_NOTIFICATION_ERROR' } });
  }
});

export { router as notificationRouter };
