const express = require('express');
const router = express.Router();

const path = require('path');
const { isPathInside } = require('../../utils/is-path-inside');
const publicFormController = require('@/controllers/coreControllers/publicFormController');
const loadCalculatorController = require('@/controllers/coreControllers/loadCalculatorController');

router.route('/forms/:slug').get(publicFormController.getForm);
router.route('/forms/:slug/submit').post(publicFormController.submit);
router.route('/builder-forms/:slug').get(publicFormController.getBuilderForm);
router.route('/builder-forms/:slug/submit').post(publicFormController.submitBuilderForm);
router.route('/load-calculator/:tenantSlug').get(loadCalculatorController.load);
router.route('/load-calculator/:tenantSlug/submit').post(loadCalculatorController.submit);

router.route('/:subPath/:directory/:file').get(function (req, res) {
  try {
    const { subPath, directory, file } = req.params;

    // Decode each parameter separately
    const decodedSubPath = decodeURIComponent(subPath);
    const decodedDirectory = decodeURIComponent(directory);
    const decodedFile = decodeURIComponent(file);

    // Define the trusted root directory
    const rootDir = path.join(__dirname, '../../public');

    // Safely join the decoded path segments
    const relativePath = path.join(decodedSubPath, decodedDirectory, decodedFile);
    const absolutePath = path.join(rootDir, relativePath);

    // Check if the resulting path stays inside rootDir
    if (!isPathInside(absolutePath, rootDir)) {
      return res.status(400).json({
        success: false,
        error: 'Invalid filepath',
      });
    }

    return res.sendFile(absolutePath, (error) => {
      if (error) {
        return res.status(404).json({
          success: false,
          result: null,
          message: 'we could not find : ' + file,
        });
      }
    });
  } catch (error) {
    return res.status(503).json({
      success: false,
      result: null,
      message: error.message,
      error: error,
    });
  }
});

module.exports = router;
