import { Router } from 'express';
import scheduledMessagesController from '../controllers/scheduledMessages.controller';
import { authenticate } from '../middleware/auth.middleware';
import { requirePermission } from '../middleware/rbac.middleware';

const router = Router();

// /rooms, /my-bot-room and /one-time MUST come before /:id to avoid being captured as an id param
router.get(
  '/rooms',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.getRooms.bind(scheduledMessagesController),
);

router.get(
  '/my-bot-room',
  authenticate,
  requirePermission('scheduled_messages:subscribe'),
  scheduledMessagesController.getMyBotRoom.bind(scheduledMessagesController),
);

router.get(
  '/one-time',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.getOneTimeMessages.bind(scheduledMessagesController),
);

router.post(
  '/one-time',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.createOneTimeMessage.bind(scheduledMessagesController),
);

router.delete(
  '/one-time/:id',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.deleteOneTimeMessage.bind(scheduledMessagesController),
);

router.get(
  '/',
  authenticate,
  requirePermission('scheduled_messages:view'),
  scheduledMessagesController.getAll.bind(scheduledMessagesController),
);

router.post(
  '/',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.create.bind(scheduledMessagesController),
);

router.get(
  '/:id',
  authenticate,
  requirePermission('scheduled_messages:view'),
  scheduledMessagesController.getById.bind(scheduledMessagesController),
);

router.patch(
  '/:id',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.update.bind(scheduledMessagesController),
);

router.delete(
  '/:id',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.delete.bind(scheduledMessagesController),
);

router.post(
  '/:id/subscribe',
  authenticate,
  requirePermission('scheduled_messages:subscribe'),
  scheduledMessagesController.subscribe.bind(scheduledMessagesController),
);

router.delete(
  '/:id/subscribe',
  authenticate,
  requirePermission('scheduled_messages:subscribe'),
  scheduledMessagesController.unsubscribe.bind(scheduledMessagesController),
);

router.post(
  '/:id/trigger',
  authenticate,
  requirePermission('scheduled_messages:edit'),
  scheduledMessagesController.triggerNow.bind(scheduledMessagesController),
);

export default router;
