import { Router } from 'express';
import { InventoryController } from '../controllers/inventory.controller';
import { requireAuth, requireRoles, requirePermissions } from '../../../shared/middleware/auth.middleware';
import { validateBody, validateQuery } from '../../../shared/validation/validation.middleware';
import {
  createTransferSchema,
  shipTransferSchema,
  receiveTransferSchema,
  cancelTransferSchema,
  listTransfersQuerySchema,
  createAdjustmentSchema,
  addAdjustmentLineSchema,
  rejectAdjustmentSchema,
  cancelAdjustmentSchema,
  listAdjustmentsQuerySchema,
} from '../validation/inventory.schema';

export function createInventoryRoutes(controller: InventoryController): Router {
  const router = Router();

  // Apply auth middleware to all routes
  router.use(requireAuth);

  // ==================== TRANSFER ROUTES ====================

  // Create transfer
  router.post(
    '/transfers',
    requirePermissions(['inventory.transfers.create']),
    validateBody(createTransferSchema),
    controller.createTransfer
  );

  // List transfers
  router.get(
    '/transfers',
    requirePermissions(['inventory.transfers.view']),
    validateQuery(listTransfersQuerySchema),
    controller.listTransfers
  );

  // Get transfer summary
  router.get(
    '/transfers/summary',
    requirePermissions(['inventory.transfers.view']),
    controller.getTransferSummary
  );

  // Get incoming transfers
  router.get(
    '/transfers/incoming',
    requirePermissions(['inventory.transfers.view']),
    controller.getIncomingTransfers
  );

  // Get transfer by ID
  router.get(
    '/transfers/:id',
    requirePermissions(['inventory.transfers.view']),
    controller.getTransfer
  );

  // Submit transfer for approval
  router.post(
    '/transfers/:id/submit',
    requirePermissions(['inventory.transfers.submit']),
    controller.submitTransfer
  );

  // Approve transfer
  router.post(
    '/transfers/:id/approve',
    requireRoles(['admin', 'warehouse_manager', 'manager']),
    controller.approveTransfer
  );

  // Ship transfer
  router.post(
    '/transfers/:id/ship',
    requirePermissions(['inventory.transfers.ship']),
    validateBody(shipTransferSchema),
    controller.shipTransfer
  );

  // Receive transfer
  router.post(
    '/transfers/:id/receive',
    requirePermissions(['inventory.transfers.receive']),
    validateBody(receiveTransferSchema),
    controller.receiveTransfer
  );

  // Cancel transfer
  router.post(
    '/transfers/:id/cancel',
    requirePermissions(['inventory.transfers.cancel']),
    validateBody(cancelTransferSchema),
    controller.cancelTransfer
  );

  // ==================== ADJUSTMENT ROUTES ====================

  // Create adjustment
  router.post(
    '/adjustments',
    requirePermissions(['inventory.adjustments.create']),
    validateBody(createAdjustmentSchema),
    controller.createAdjustment
  );

  // List adjustments
  router.get(
    '/adjustments',
    requirePermissions(['inventory.adjustments.view']),
    validateQuery(listAdjustmentsQuerySchema),
    controller.listAdjustments
  );

  // Get adjustment summary
  router.get(
    '/adjustments/summary',
    requirePermissions(['inventory.adjustments.view']),
    controller.getAdjustmentSummary
  );

  // Get adjustment by ID
  router.get(
    '/adjustments/:id',
    requirePermissions(['inventory.adjustments.view']),
    controller.getAdjustment
  );

  // Add line to adjustment
  router.post(
    '/adjustments/:id/lines',
    requirePermissions(['inventory.adjustments.edit']),
    validateBody(addAdjustmentLineSchema),
    controller.addAdjustmentLine
  );

  // Submit adjustment for approval
  router.post(
    '/adjustments/:id/submit',
    requirePermissions(['inventory.adjustments.submit']),
    controller.submitAdjustment
  );

  // Approve adjustment
  router.post(
    '/adjustments/:id/approve',
    requireRoles(['admin', 'warehouse_manager', 'manager']),
    controller.approveAdjustment
  );

  // Reject adjustment
  router.post(
    '/adjustments/:id/reject',
    requireRoles(['admin', 'warehouse_manager', 'manager']),
    validateBody(rejectAdjustmentSchema),
    controller.rejectAdjustment
  );

  // Post adjustment
  router.post(
    '/adjustments/:id/post',
    requireRoles(['admin', 'warehouse_manager']),
    controller.postAdjustment
  );

  // Cancel adjustment
  router.post(
    '/adjustments/:id/cancel',
    requirePermissions(['inventory.adjustments.cancel']),
    validateBody(cancelAdjustmentSchema),
    controller.cancelAdjustment
  );

  return router;
}
