import { Router, Request, Response, NextFunction } from 'express';
import { mapSettingsService } from './settings.service';
import { updateMapSettingsSchema } from './settings.schemas';
import { validate } from '../../../middleware/validate';
import { authenticate } from '../../../middleware/auth.middleware';
import { requireRole } from '../../../middleware/rbac.middleware';
import { MAP_ROLES } from '../../../utils/roles';

const router = Router();

// GET /api/map/settings — public (needed for map center/zoom)
router.get(
  '/',
  async (_req: Request, res: Response, next: NextFunction) => {
    try {
      const settings = await mapSettingsService.get();
      res.json(settings);
    } catch (err) {
      next(err);
    }
  }
);

// PUT /api/map/settings — admin only
router.put(
  '/',
  authenticate,
  requireRole(...MAP_ROLES),
  validate(updateMapSettingsSchema),
  async (req: Request, res: Response, next: NextFunction) => {
    try {
      const settings = await mapSettingsService.update(req.body);
      res.json(settings);
    } catch (err) {
      next(err);
    }
  }
);

export { router as mapSettingsRouter };
