const express = require('express');
const router = express.Router();
const serverError = require('../utils/serverError');
const database_manager = require('../src/database/database_manager');
const storage_manager = require('../src/data/storage_manager');

router.get('/projects', async (req, res) => {
   try {
      const projects = await database_manager.project.get_all_projects();
      res.json(projects);
   } catch (error) {
      serverError.sendError('Error getting all projects:', res, error, 500);
   }
});

router.get('/projects/:id', async (req, res) => {
   const projectId = req.params.id;
   if (!projectId || isNaN(projectId)) {
      return res.status(400).json({ error: 'Invalid project ID' });
   }
   try {
      const project = await database_manager.project.get_project_by_id(projectId);
      res.json(project);
   } catch (error) {
      serverError.sendError('Error getting project by ID:', res, error, 500);
   }
});

router.get('/projects/:id/videos', async (req, res) => {
   const projectId = req.params.id;
   if (!projectId || isNaN(projectId)) {
      return res.status(400).json({ error: 'Invalid project ID' });
   }
   try {
      const videos = await database_manager.video.get_videos_by_project_id(projectId);
      if (videos.length === 0) {
         return res.status(404).json({ error: 'No videos found for this project' });
      }
      res.json(videos);
   } catch (error) {
      serverError.sendError('Error getting videos by project ID:', res, error, 500);
   }
});

router.get('/projects/:id/measurements', async (req, res) => {
   const projectId = req.params.id;
   if (!projectId || isNaN(projectId)) {
      return res.status(400).json({ error: 'Invalid project ID' });
   }
   try {
      const measurements = await database_manager.measurement.get_measurements_by_project_id(projectId);
      if (measurements.length === 0) {
         return res.status(404).json({ error: 'No measurements found for this project' });
      }
      res.json(measurements);
   } catch (error) {
      serverError.sendError('Error getting measurements by project ID:', res, error, 500);
   }
});

router.post('/projects', async (req, res) => {
   const { name, description } = req.body;
   if (!name || !description) {
      return res.status(400).json({ error: 'Name and description are required' });
   }
   try {
      const date = new Date();
      const default_status = 0;
      const project = await database_manager.project.create_project(name, description, date, default_status);
      storage_manager.project.createProjectDirectory(project.id);
      res.status(201).json({ message: 'Project added successfully', id: project.id });
   } catch (error) {
      serverError.sendError('Error creating project:', res, error, 500);
   }
});

router.delete('/projects/:id', async (req, res) => {
   const projectId = req.params.id;
   if (!projectId || isNaN(projectId)) {
      return res.status(400).json({ error: 'Invalid project ID' });
   }
   try {
      storage_manager.project.deleteProjectDirectory(projectId);
      await database_manager.project.delete_project_by_id(projectId);
      res.status(200).json({ message: 'Project deleted successfully', id: projectId });
   } catch (error) {
      serverError.sendError('Error deleting project:', res, error, 500);
   }
});

module.exports = router;
