import { Router } from 'express';
import * as stockController from '../controllers/stockController.js';
import { extractUser, requireAdmin, requireUser } from '../middleware/authMiddleware.js';

const router = Router();

// Retrieve all items
router.get('/', extractUser, stockController.getStockItems);

// Retrieve Item Details
router.get('/:id', extractUser, stockController.getStockItemById);

// Retrieve movements for a specific item
router.get('/:id/movements', extractUser, stockController.getStockMovements);

// Retrieve logs for a specific item
router.get('/:id/logs', extractUser, stockController.getStockAuditLogs);

// Create (Entry)
router.post('/', extractUser, requireUser, stockController.createStockItem);

// Update Details (No quantity)
router.put('/:id', extractUser, requireAdmin, stockController.updateStockItem);

// Technical Adjustment (Baixa Técnica / Correção)
router.post('/:id/adjust', extractUser, requireAdmin, stockController.adjustStock);

// Consumption (Baixa por Obra)
router.post('/:id/consume', extractUser, requireUser, stockController.consumeStock);

// Delete Stock Item (and its movements)
router.delete('/:id', extractUser, requireAdmin, stockController.deleteStockItem);

// -----------------------------------------------------------
// Movement CRUD
// -----------------------------------------------------------
router.put('/movements/:id', extractUser, requireAdmin, stockController.updateStockMovement);
router.delete('/movements/:id', extractUser, requireAdmin, stockController.deleteStockMovement);

export default router;
