import { Router } from 'express';
import { summaryController } from '@controllers/summary.controller';
import { authenticateToken } from '@middlewares/auth.middleware';
import { asyncHandler } from '@middlewares/errorHandler.middleware';

const router = Router();

// All routes require authentication
router.use(authenticateToken);

// Create summary for a closed request
router.post(
  '/',
  asyncHandler(summaryController.createSummary.bind(summaryController))
);

// List summaries shared with current user (MUST come before /:summaryId)
router.get(
  '/shared',
  asyncHandler(summaryController.listSharedSummaries.bind(summaryController))
);

// Mark shared summary as viewed (MUST come before /:summaryId)
router.patch(
  '/shared/:sharedSummaryId/view',
  asyncHandler(summaryController.markAsViewed.bind(summaryController))
);

// List summaries created by current user (MUST come before /:summaryId)
router.get(
  '/my',
  asyncHandler(summaryController.listMySummaries.bind(summaryController))
);

// Get summary by requestId (MUST come before /:summaryId)
router.get(
  '/request/:requestId',
  asyncHandler(summaryController.getSummaryByRequestId.bind(summaryController))
);

// Regenerate summary for a request (MUST come before /:summaryId)
// Access: Initiator or Admin/Management users
router.post(
  '/request/:requestId/regenerate',
  asyncHandler(summaryController.regenerateSummary.bind(summaryController))
);

// Share summary with users (MUST come before /:summaryId)
router.post(
  '/:summaryId/share',
  asyncHandler(summaryController.shareSummary.bind(summaryController))
);

// Get shared recipients for a summary (MUST come before /:summaryId)
router.get(
  '/:summaryId/recipients',
  asyncHandler(summaryController.getSharedRecipients.bind(summaryController))
);

// Get summary details (MUST come last - it's a catch-all for UUIDs)
router.get(
  '/:summaryId',
  asyncHandler(summaryController.getSummaryDetails.bind(summaryController))
);

export default router;

