const { Router } = require('express');
const { UPLOAD_FS_DIR } = require('../constants');
const path = require('path');

const router = Router();

/**
 * @swagger
 * /download/{id}:
 *   get:
 *     tags: [Download]
 *     summary: Download an uploaded image file
 *     description: Downloads the original image file by filename
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *           example: "abc123.jpg"
 *         description: Filename of the image to download
 *     responses:
 *       200:
 *         description: File download initiated
 *         content:
 *           image/*:
 *             schema:
 *               type: string
 *               format: binary
 *       404:
 *         description: File not found
 */
router.get('/download/:id', (req, res) => {
    /*
        #swagger.tags = ['Download']
        #swagger.summary = 'Download original image'
        #swagger.parameters['id'] = {
            in: 'path',
            required: true,
            type: 'string',
            description: 'Filename of the uploaded image'
        }
        #swagger.responses[200] = { description: 'Binary image response' }
        #swagger.responses[404] = { description: 'File not found' }
    */
    const filePath = path.join(__dirname, '..', UPLOAD_FS_DIR, req.params.id);
    res.download(filePath);
});

module.exports = router;
