import { Router } from 'express';
import { inventoryController } from './inventory.controller.js';
import { valuationController } from './valuation.controller.js';
import { authenticate } from '../../shared/middleware/auth.middleware.js';
import { requireAccess } from '../../shared/middleware/rbac.middleware.js';

const router = Router();

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

// ========== PRODUCTS ==========
router.get('/products', (req, res, next) => inventoryController.getProducts(req, res, next));

router.get('/products/:id', (req, res, next) => inventoryController.getProduct(req, res, next));

router.get('/products/:id/stock', (req, res, next) => inventoryController.getProductStock(req, res, next));

router.post('/products', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'products:create' }), (req, res, next) =>
  inventoryController.createProduct(req, res, next)
);

router.put('/products/:id', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'products:update' }), (req, res, next) =>
  inventoryController.updateProduct(req, res, next)
);

router.delete('/products/:id', requireAccess({ roles: ['admin'], permission: 'products:delete' }), (req, res, next) =>
  inventoryController.deleteProduct(req, res, next)
);

// ========== WAREHOUSES ==========
router.get('/warehouses', (req, res, next) => inventoryController.getWarehouses(req, res, next));

router.get('/warehouses/:id', (req, res, next) => inventoryController.getWarehouse(req, res, next));

router.get('/warehouses/:id/locations', (req, res, next) => inventoryController.getWarehouseLocations(req, res, next));

router.get('/warehouses/:id/stock', (req, res, next) => inventoryController.getWarehouseStock(req, res, next));

router.post('/warehouses', requireAccess({ roles: ['admin'], permission: 'warehouses:create' }), (req, res, next) =>
  inventoryController.createWarehouse(req, res, next)
);

router.put('/warehouses/:id', requireAccess({ roles: ['admin'], permission: 'warehouses:update' }), (req, res, next) =>
  inventoryController.updateWarehouse(req, res, next)
);

router.delete('/warehouses/:id', requireAccess({ roles: ['admin'], permission: 'warehouses:delete' }), (req, res, next) =>
  inventoryController.deleteWarehouse(req, res, next)
);

// ========== LOCATIONS ==========
router.get('/locations', (req, res, next) => inventoryController.getLocations(req, res, next));

router.get('/locations/:id', (req, res, next) => inventoryController.getLocation(req, res, next));

router.get('/locations/:id/stock', (req, res, next) => inventoryController.getLocationStock(req, res, next));

router.post('/locations', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'warehouses:update' }), (req, res, next) =>
  inventoryController.createLocation(req, res, next)
);

router.put('/locations/:id', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'warehouses:update' }), (req, res, next) =>
  inventoryController.updateLocation(req, res, next)
);

// ========== PICKINGS ==========
router.get('/pickings', (req, res, next) => inventoryController.getPickings(req, res, next));

router.get('/pickings/:id', (req, res, next) => inventoryController.getPicking(req, res, next));

router.post('/pickings', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'stock_moves:create' }), (req, res, next) =>
  inventoryController.createPicking(req, res, next)
);

router.post('/pickings/:id/confirm', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'stock_moves:validate' }), (req, res, next) =>
  inventoryController.confirmPicking(req, res, next)
);

router.post('/pickings/:id/validate', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'stock_moves:validate' }), (req, res, next) =>
  inventoryController.validatePicking(req, res, next)
);

router.post('/pickings/:id/cancel', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'stock_moves:cancel' }), (req, res, next) =>
  inventoryController.cancelPicking(req, res, next)
);

router.delete('/pickings/:id', requireAccess({ roles: ['admin'], permission: 'stock_moves:delete' }), (req, res, next) =>
  inventoryController.deletePicking(req, res, next)
);

// ========== LOTS ==========
router.get('/lots', (req, res, next) => inventoryController.getLots(req, res, next));

router.get('/lots/:id', (req, res, next) => inventoryController.getLot(req, res, next));

router.get('/lots/:id/movements', (req, res, next) => inventoryController.getLotMovements(req, res, next));

router.post('/lots', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:update' }), (req, res, next) =>
  inventoryController.createLot(req, res, next)
);

router.put('/lots/:id', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:update' }), (req, res, next) =>
  inventoryController.updateLot(req, res, next)
);

router.delete('/lots/:id', requireAccess({ roles: ['admin'], permission: 'inventory:update' }), (req, res, next) =>
  inventoryController.deleteLot(req, res, next)
);

// ========== ADJUSTMENTS ==========
router.get('/adjustments', (req, res, next) => inventoryController.getAdjustments(req, res, next));

router.get('/adjustments/:id', (req, res, next) => inventoryController.getAdjustment(req, res, next));

router.post('/adjustments', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.createAdjustment(req, res, next)
);

router.put('/adjustments/:id', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.updateAdjustment(req, res, next)
);

// Adjustment lines
router.post('/adjustments/:id/lines', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.addAdjustmentLine(req, res, next)
);

router.put('/adjustments/:id/lines/:lineId', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.updateAdjustmentLine(req, res, next)
);

router.delete('/adjustments/:id/lines/:lineId', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.removeAdjustmentLine(req, res, next)
);

// Adjustment workflow
router.post('/adjustments/:id/confirm', requireAccess({ roles: ['admin', 'manager', 'warehouse'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.confirmAdjustment(req, res, next)
);

router.post('/adjustments/:id/validate', requireAccess({ roles: ['admin', 'manager'], permission: 'stock_count:validate' }), (req, res, next) =>
  inventoryController.validateAdjustment(req, res, next)
);

router.post('/adjustments/:id/cancel', requireAccess({ roles: ['admin', 'manager'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.cancelAdjustment(req, res, next)
);

router.delete('/adjustments/:id', requireAccess({ roles: ['admin'], permission: 'inventory:adjust' }), (req, res, next) =>
  inventoryController.deleteAdjustment(req, res, next)
);

// ========== VALUATION ==========
router.get('/valuation/cost', (req, res, next) => valuationController.getProductCost(req, res, next));

router.get('/valuation/report', (req, res, next) => valuationController.getCompanyReport(req, res, next));

router.get('/valuation/products/:productId/summary', (req, res, next) =>
  valuationController.getProductSummary(req, res, next)
);

router.get('/valuation/products/:productId/layers', (req, res, next) =>
  valuationController.getProductLayers(req, res, next)
);

router.post('/valuation/layers', requireAccess({ roles: ['admin', 'manager'], permission: 'inventory:update' }), (req, res, next) =>
  valuationController.createLayer(req, res, next)
);

router.post('/valuation/consume', requireAccess({ roles: ['admin', 'manager'], permission: 'inventory:update' }), (req, res, next) =>
  valuationController.consumeFifo(req, res, next)
);

export default router;
