const express = require('express');
const router = express.Router();
const database_manager = require('../src/database/database_manager');

router.get('/measurements', async (req, res) => {
   const measurements = await database_manager.measurement.get_all_measurements();
   if (!measurements) {
      return res.status(404).json({ error: 'No measurements found' });
   }
   res.json(measurements);
});

router.get('/measurements/:id', async (req, res) => {
   const measurement = await database_manager.measurement.get_measurement_by_id(req.params.id);
   if (!measurement) {
      return res.status(404).json({ error: 'Measurement not found' });
   }
   res.json(measurement);
});

router.get('/measurements/:projectId/:orderId', async (req, res) => {
   const measurement = await database_manager.measurement.get_measurement_by_project_and_order_id(req.params.projectId, req.params.orderId);
   if (!measurement) {
      return res.status(404).json({ error: 'Measurement not found' });
   }
   res.json(measurement);
});

router.delete('/measurements/:id', async (req, res) => {
   const measurement = await database_manager.measurement.delete_measurement_by_id(req.params.id);
   if (!measurement) {
      return res.status(404).json({ error: 'Measurement not found' });
   }
   res.json({ message: 'Measurement deleted successfully', id: measurement.id });
});

router.delete('/measurements/:projectId/:orderId', async (req, res) => {
   const measurement = await database_manager.measurement.delete_measurement_by_project_and_order_id(req.params.projectId, req.params.orderId);
   if (!measurement) {
      return res.status(404).json({ error: 'Measurement not found' });
   }
   res.json({ message: 'Measurement deleted successfully', id: measurement.id });
});

module.exports = router;
