import { Router } from 'express';
import { DMSWebhookController } from '../controllers/dmsWebhook.controller';
import { asyncHandler } from '../middlewares/errorHandler.middleware';

const router = Router();
const webhookController = new DMSWebhookController();

/**
 * @route   GET /api/v1/webhooks/dms/health
 * @desc    Health check endpoint for webhook routes (for testing)
 * @access  Public
 */
router.get('/health', (_req, res) => {
  res.status(200).json({
    status: 'OK',
    message: 'DMS Webhook routes are active',
    endpoints: {
      invoice: 'POST /api/v1/webhooks/dms/invoice',
      creditNote: 'POST /api/v1/webhooks/dms/credit-note'
    }
  });
});

/**
 * @route   POST /api/v1/webhooks/dms/invoice
 * @desc    Webhook endpoint for DMS invoice generation callbacks
 * @access  Public (authenticated via webhook signature)
 * @note    This endpoint is called by DMS system after invoice generation
 */
router.post(
  '/invoice',
  asyncHandler(webhookController.handleInvoiceWebhook.bind(webhookController))
);

/**
 * @route   POST /api/v1/webhooks/dms/credit-note
 * @desc    Webhook endpoint for DMS credit note generation callbacks
 * @access  Public (authenticated via webhook signature)
 * @note    This endpoint is called by DMS system after credit note generation
 */
router.post(
  '/credit-note',
  asyncHandler(webhookController.handleCreditNoteWebhook.bind(webhookController))
);

export default router;

