const express = require('express');
const router = express.Router();
const path = require('path');
const fs = require('fs');
const dbTester = require('../test/tester');
const db = require('../db');
const serverError = require('../utils/serverError');

/**
 * @swagger
 * /smile:
 *   get:
 *     summary: Retrieve a smile image
 *     responses:
 *       200:
 *         description: A smile image
 *         content:
 *           image/jpeg:
 *             schema:
 *               type: string
 *               format: binary
 *       404:
 *         description: Image not found
 */
router.get('/smile', (req, res) => {
   const imagePath = dbTester.getSmileImage();
   fs.access(imagePath, fs.constants.F_OK, (err) => {
      if (err) {
         console.error('Image not found:', err);
         return res.status(404).json({ error: 'Image not found' });
      }
      res.sendFile(imagePath);
   });
});

/**
 * @swagger
 * /images/{projectId}/{orderId}:
 *   get:
 *     summary: Retrieve an image by project and order ID
 *     parameters:
 *       - in: path
 *         name: projectId
 *         required: true
 *         schema:
 *           type: string
 *         description: The project ID
 *       - in: path
 *         name: orderId
 *         required: true
 *         schema:
 *           type: string
 *         description: The order ID
 *     responses:
 *       200:
 *         description: An image file
 *         content:
 *           application/octet-stream:
 *             schema:
 *               type: string
 *               format: binary
 *       404:
 *         description: Image not found
 */
router.get('/images/:projectId/:orderId', (req, res) => {
    const projectId = req.params.projectId;
    const orderId = req.params.orderId;
    const query = 'SELECT path FROM public.measurements WHERE project_id = $1 AND order_id = $2';
    db.query(query, [projectId, orderId], (err, results) => {
         if (err) {
             return serverError.sendError('Error getting image:', res, err);
         }
         if (results.rows.length === 0) {
             return res.status(404).json({ error: 'Image not found' });
         }
         const imagePath = results.rows[0].path;
         fs.access(imagePath, fs.constants.F_OK, (err) => {
             if (err) {
                  console.error('Image not found:', err);
                  return res.status(404).json({ error: 'Image not found' });
             }
             res.download(imagePath);
         });
    });
});


/**
 * @swagger
 * /images/{measurementId}:
 *   get:
 *     summary: Retrieve an image by measurement ID
 *     parameters:
 *       - in: path
 *         name: measurementId
 *         required: true
 *         schema:
 *           type: string
 *         description: The measurement ID
 *     responses:
 *       200:
 *         description: An image file
 *         content:
 *           application/octet-stream:
 *             schema:
 *               type: string
 *               format: binary
 *       404:
 *         description: Image not found
 */
router.get('/images/:measurementId', (req, res) => {
    const measurementId = req.params.measurementId;
    const query = 'SELECT path FROM public.measurements WHERE id = $1';
    db.query(query, [measurementId], (err, results) => {
        if (err) {
            return serverError.sendError('Error getting image:', res, err);
        }
        if (results.rows.length === 0) {
            return res.status(404).json({ error: 'Image not found' });
        }
        const imagePath = results.rows[0].path;
        fs.access(imagePath, fs.constants.F_OK, (err) => {
            if (err) {
                console.error('Image not found:', err);
                return res.status(404).json({ error: 'Image not found' });
            }
            res.download(imagePath);
        });
    });
});

module.exports = router;
