import { Router } from 'express';
import { inventoryController } from './inventory.controller.js';
import { valuationController } from './valuation.controller.js';
import { authenticate, requireRoles } from '../../shared/middleware/auth.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', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.createProduct(req, res, next)
);

router.put('/products/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.updateProduct(req, res, next)
);

router.delete('/products/:id', requireRoles('admin', 'super_admin'), (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', requireRoles('admin', 'super_admin'), (req, res, next) =>
  inventoryController.createWarehouse(req, res, next)
);

router.put('/warehouses/:id', requireRoles('admin', 'super_admin'), (req, res, next) =>
  inventoryController.updateWarehouse(req, res, next)
);

router.delete('/warehouses/:id', requireRoles('admin', 'super_admin'), (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', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.createLocation(req, res, next)
);

router.put('/locations/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (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', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.createPicking(req, res, next)
);

router.post('/pickings/:id/confirm', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.confirmPicking(req, res, next)
);

router.post('/pickings/:id/validate', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.validatePicking(req, res, next)
);

router.post('/pickings/:id/cancel', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.cancelPicking(req, res, next)
);

router.delete('/pickings/:id', requireRoles('admin', 'super_admin'), (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', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.createLot(req, res, next)
);

router.put('/lots/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.updateLot(req, res, next)
);

router.delete('/lots/:id', requireRoles('admin', 'super_admin'), (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', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.createAdjustment(req, res, next)
);

router.put('/adjustments/:id', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.updateAdjustment(req, res, next)
);

// Adjustment lines
router.post('/adjustments/:id/lines', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.addAdjustmentLine(req, res, next)
);

router.put('/adjustments/:id/lines/:lineId', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.updateAdjustmentLine(req, res, next)
);

router.delete('/adjustments/:id/lines/:lineId', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.removeAdjustmentLine(req, res, next)
);

// Adjustment workflow
router.post('/adjustments/:id/confirm', requireRoles('admin', 'manager', 'warehouse', 'super_admin'), (req, res, next) =>
  inventoryController.confirmAdjustment(req, res, next)
);

router.post('/adjustments/:id/validate', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  inventoryController.validateAdjustment(req, res, next)
);

router.post('/adjustments/:id/cancel', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  inventoryController.cancelAdjustment(req, res, next)
);

router.delete('/adjustments/:id', requireRoles('admin', 'super_admin'), (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', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  valuationController.createLayer(req, res, next)
);

router.post('/valuation/consume', requireRoles('admin', 'manager', 'super_admin'), (req, res, next) =>
  valuationController.consumeFifo(req, res, next)
);

export default router;
