import { Router } from 'express';
import { purchasesController } from '../controllers/purchases.controller';
import { authMiddleware, requireRoles, requirePermissions } from '../../../shared/middleware/auth.middleware';
import { tenantMiddleware } from '../../../shared/middleware/tenant.middleware';
import { branchMiddleware, validateBranchMiddleware, requireBranchCapability } from '../../../shared/middleware/branch.middleware';
import { AuthenticatedRequest } from '../../../shared/types';

const router = Router();

// All routes require tenant and authentication
router.use(tenantMiddleware);
router.use(authMiddleware);

// ==================== SUPPLIER ORDER ROUTES ====================

// Routes that require branch context
router.use('/orders', branchMiddleware);
router.use('/orders', validateBranchMiddleware);

// Create supplier order
router.post(
  '/orders',
  requirePermissions('purchases:create'),
  (req, res, next) => purchasesController.createOrder(req as AuthenticatedRequest, res, next)
);

// List supplier orders
router.get(
  '/orders',
  requirePermissions('purchases:read'),
  (req, res, next) => purchasesController.listOrders(req as AuthenticatedRequest, res, next)
);

// Get order statistics
router.get(
  '/orders/stats',
  requirePermissions('purchases:read'),
  (req, res, next) => purchasesController.getOrderStats(req as AuthenticatedRequest, res, next)
);

// Get supplier order by ID
router.get(
  '/orders/:id',
  requirePermissions('purchases:read'),
  (req, res, next) => purchasesController.getOrder(req as AuthenticatedRequest, res, next)
);

// Update supplier order
router.put(
  '/orders/:id',
  requirePermissions('purchases:update'),
  (req, res, next) => purchasesController.updateOrder(req as AuthenticatedRequest, res, next)
);

// Submit order for approval
router.post(
  '/orders/:id/submit',
  requirePermissions('purchases:update'),
  (req, res, next) => purchasesController.submitForApproval(req as AuthenticatedRequest, res, next)
);

// Approve order (requires manager+ role)
router.post(
  '/orders/:id/approve',
  requireRoles('admin', 'manager'),
  requirePermissions('purchases:approve'),
  (req, res, next) => purchasesController.approveOrder(req as AuthenticatedRequest, res, next)
);

// Reject order (requires manager+ role)
router.post(
  '/orders/:id/reject',
  requireRoles('admin', 'manager'),
  requirePermissions('purchases:approve'),
  (req, res, next) => purchasesController.rejectOrder(req as AuthenticatedRequest, res, next)
);

// Send order to supplier
router.post(
  '/orders/:id/send',
  requirePermissions('purchases:update'),
  (req, res, next) => purchasesController.sendToSupplier(req as AuthenticatedRequest, res, next)
);

// Supplier confirmation
router.post(
  '/orders/:id/confirm',
  requirePermissions('purchases:update'),
  (req, res, next) => purchasesController.supplierConfirmation(req as AuthenticatedRequest, res, next)
);

// Cancel order (requires supervisor+ role)
router.post(
  '/orders/:id/cancel',
  requireRoles('admin', 'manager', 'supervisor'),
  requirePermissions('purchases:cancel'),
  (req, res, next) => purchasesController.cancelOrder(req as AuthenticatedRequest, res, next)
);

// ==================== GOODS RECEIPT ROUTES ====================

// Routes that require branch context
router.use('/receipts', branchMiddleware);
router.use('/receipts', validateBranchMiddleware);

// Create goods receipt
router.post(
  '/receipts',
  requirePermissions('receipts:create'),
  (req, res, next) => purchasesController.createReceipt(req as AuthenticatedRequest, res, next)
);

// Create receipt from order
router.post(
  '/receipts/from-order',
  requirePermissions('receipts:create'),
  (req, res, next) => purchasesController.createReceiptFromOrder(req as AuthenticatedRequest, res, next)
);

// List goods receipts
router.get(
  '/receipts',
  requirePermissions('receipts:read'),
  (req, res, next) => purchasesController.listReceipts(req as AuthenticatedRequest, res, next)
);

// Get receipt statistics
router.get(
  '/receipts/stats',
  requirePermissions('receipts:read'),
  (req, res, next) => purchasesController.getReceiptStats(req as AuthenticatedRequest, res, next)
);

// Get goods receipt by ID
router.get(
  '/receipts/:id',
  requirePermissions('receipts:read'),
  (req, res, next) => purchasesController.getReceipt(req as AuthenticatedRequest, res, next)
);

// Update goods receipt
router.put(
  '/receipts/:id',
  requirePermissions('receipts:update'),
  (req, res, next) => purchasesController.updateReceipt(req as AuthenticatedRequest, res, next)
);

// Complete quality check
router.post(
  '/receipts/:id/quality-check',
  requirePermissions('receipts:quality'),
  (req, res, next) => purchasesController.completeQualityCheck(req as AuthenticatedRequest, res, next)
);

// Post receipt
router.post(
  '/receipts/:id/post',
  requireRoles('admin', 'manager', 'supervisor'),
  requirePermissions('receipts:post'),
  (req, res, next) => purchasesController.postReceipt(req as AuthenticatedRequest, res, next)
);

// Cancel receipt
router.post(
  '/receipts/:id/cancel',
  requireRoles('admin', 'manager', 'supervisor'),
  requirePermissions('receipts:cancel'),
  (req, res, next) => purchasesController.cancelReceipt(req as AuthenticatedRequest, res, next)
);

// ==================== PURCHASE SUGGESTION ROUTES ====================

// Routes that require branch context
router.use('/suggestions', branchMiddleware);
router.use('/suggestions', validateBranchMiddleware);

// Create suggestion
router.post(
  '/suggestions',
  requirePermissions('suggestions:create'),
  (req, res, next) => purchasesController.createSuggestion(req as AuthenticatedRequest, res, next)
);

// Generate suggestions
router.post(
  '/suggestions/generate',
  requireRoles('admin', 'manager'),
  requirePermissions('suggestions:generate'),
  (req, res, next) => purchasesController.generateSuggestions(req as AuthenticatedRequest, res, next)
);

// Bulk review suggestions
router.post(
  '/suggestions/bulk-review',
  requireRoles('admin', 'manager'),
  requirePermissions('suggestions:approve'),
  (req, res, next) => purchasesController.bulkReviewSuggestions(req as AuthenticatedRequest, res, next)
);

// Convert suggestions to orders
router.post(
  '/suggestions/convert',
  requireRoles('admin', 'manager'),
  requirePermissions('suggestions:convert'),
  (req, res, next) => purchasesController.convertToOrders(req as AuthenticatedRequest, res, next)
);

// List suggestions
router.get(
  '/suggestions',
  requirePermissions('suggestions:read'),
  (req, res, next) => purchasesController.listSuggestions(req as AuthenticatedRequest, res, next)
);

// Get suggestion statistics
router.get(
  '/suggestions/stats',
  requirePermissions('suggestions:read'),
  (req, res, next) => purchasesController.getSuggestionStats(req as AuthenticatedRequest, res, next)
);

// Get suggestion by ID
router.get(
  '/suggestions/:id',
  requirePermissions('suggestions:read'),
  (req, res, next) => purchasesController.getSuggestion(req as AuthenticatedRequest, res, next)
);

// Update suggestion
router.put(
  '/suggestions/:id',
  requirePermissions('suggestions:update'),
  (req, res, next) => purchasesController.updateSuggestion(req as AuthenticatedRequest, res, next)
);

// Review suggestion
router.post(
  '/suggestions/:id/review',
  requireRoles('admin', 'manager'),
  requirePermissions('suggestions:approve'),
  (req, res, next) => purchasesController.reviewSuggestion(req as AuthenticatedRequest, res, next)
);

// Delete suggestion
router.delete(
  '/suggestions/:id',
  requireRoles('admin', 'manager'),
  requirePermissions('suggestions:delete'),
  (req, res, next) => purchasesController.deleteSuggestion(req as AuthenticatedRequest, res, next)
);

export default router;
