import { Router } from 'express';
import issueController from '../controllers/issue.controller';
import { authenticate } from '../middleware/auth.middleware';
import { requirePermission } from '../middleware/rbac.middleware';
import { uploadIssue } from '../middleware/upload';

const router = Router();

// --- Widget summary route (BEFORE /:id) ---
router.get(
  '/widget-summary',
  authenticate,
  requirePermission('issues:view_all'),
  issueController.getWidgetSummary.bind(issueController)
);

// --- Statuses CRUD (BEFORE /:id) ---
router.get('/statuses', authenticate, issueController.getIssueStatuses.bind(issueController));
router.post('/statuses', authenticate, requirePermission('issues:edit_settings'), issueController.createIssueStatus.bind(issueController));
router.patch('/statuses/:id', authenticate, requirePermission('issues:edit_settings'), issueController.updateIssueStatus.bind(issueController));
router.delete('/statuses/:id', authenticate, requirePermission('issues:edit_settings'), issueController.deleteIssueStatus.bind(issueController));

// --- Priorities CRUD (BEFORE /:id) ---
router.get('/priorities', authenticate, issueController.getIssuePriorities.bind(issueController));
router.post('/priorities', authenticate, requirePermission('issues:edit_settings'), issueController.createIssuePriority.bind(issueController));
router.patch('/priorities/:id', authenticate, requirePermission('issues:edit_settings'), issueController.updateIssuePriority.bind(issueController));
router.delete('/priorities/:id', authenticate, requirePermission('issues:edit_settings'), issueController.deleteIssuePriority.bind(issueController));

// --- Type management routes (BEFORE /:id to avoid conflict) ---
router.get(
  '/typen',
  authenticate,
  issueController.getTypes.bind(issueController)
);

router.post(
  '/typen',
  authenticate,
  requirePermission('issues:edit_settings'),
  issueController.createType.bind(issueController)
);

router.patch(
  '/typen/:id',
  authenticate,
  requirePermission('issues:edit_settings'),
  issueController.updateType.bind(issueController)
);

router.delete(
  '/typen/:id',
  authenticate,
  requirePermission('issues:edit_settings'),
  issueController.deleteType.bind(issueController)
);

// --- Members route ---
router.get(
  '/members',
  authenticate,
  issueController.getMembers.bind(issueController)
);

// --- File management (BEFORE /:id to avoid conflict) ---
router.delete(
  '/files/:fileId',
  authenticate,
  issueController.deleteFile.bind(issueController)
);

// --- Issue CRUD ---
router.get(
  '/',
  authenticate,
  issueController.getIssues.bind(issueController)
);

router.post(
  '/',
  authenticate,
  requirePermission('issues:create'),
  issueController.createIssue.bind(issueController)
);

router.get(
  '/:id/comments',
  authenticate,
  issueController.getComments.bind(issueController)
);

router.get(
  '/:id/history',
  authenticate,
  issueController.getHistory.bind(issueController)
);

router.post(
  '/:id/comments',
  authenticate,
  issueController.addComment.bind(issueController)
);

router.post(
  '/:id/files',
  authenticate,
  uploadIssue.single('file'),
  issueController.uploadFile.bind(issueController)
);

router.get(
  '/:id/files',
  authenticate,
  issueController.getFiles.bind(issueController)
);

router.get(
  '/:id',
  authenticate,
  issueController.getIssue.bind(issueController)
);

router.patch(
  '/:id',
  authenticate,
  issueController.updateIssue.bind(issueController)
);

router.delete(
  '/:id',
  authenticate,
  issueController.deleteIssue.bind(issueController)
);

export default router;
