// Typeform router — ESM conversion of typeform-server/routes/typeform.routes.js.
// All routes read-only — authenticated-only is sufficient; typeform_write reserved
// for future write endpoints (per migration 005).

import express from 'express';
import { TypeformService } from '../../services/typeform/typeform.service.js';

export function createTypeformRouter({ redis }) {
  const router = express.Router();
  const typeform = new TypeformService(redis);

  router.get('/forms/:formId/responses', async (req, res) => {
    try {
      const { formId } = req.params;
      const filters = req.query;
      if (!formId) {
        return res.status(400).json({ error: 'Missing form ID', details: 'The form ID parameter is required' });
      }
      const data = await typeform.getFormResponsesWithFilters(formId, filters);
      if (!data) {
        return res.status(404).json({ error: 'No data found', details: `No responses found for form ${formId}` });
      }
      res.json(data);
    } catch (error) {
      console.error('Form responses error:', {
        formId: req.params.formId,
        filters: req.query,
        error: error.message,
        response: error.response?.data,
      });
      if (error.response?.status === 401) {
        return res.status(401).json({ error: 'Authentication failed', details: 'Invalid Typeform API credentials' });
      }
      if (error.response?.status === 404) {
        return res.status(404).json({ error: 'Not found', details: `Form '${req.params.formId}' not found` });
      }
      if (error.response?.status === 400) {
        return res.status(400).json({
          error: 'Invalid request',
          details: error.response?.data?.message || 'The request was invalid',
          data: error.response?.data,
        });
      }
      res.status(500).json({
        error: 'Failed to fetch form responses',
        details: error.response?.data?.message || error.message,
        data: error.response?.data,
      });
    }
  });

  router.get('/forms/:formId/insights', async (req, res) => {
    try {
      const { formId } = req.params;
      if (!formId) {
        return res.status(400).json({ error: 'Missing form ID', details: 'The form ID parameter is required' });
      }
      const data = await typeform.getFormInsights(formId);
      if (!data) {
        return res.status(404).json({ error: 'No data found', details: `No insights found for form ${formId}` });
      }
      res.json(data);
    } catch (error) {
      console.error('Form insights error:', {
        formId: req.params.formId,
        error: error.message,
        response: error.response?.data,
      });
      if (error.response?.status === 401) {
        return res.status(401).json({ error: 'Authentication failed', details: 'Invalid Typeform API credentials' });
      }
      if (error.response?.status === 404) {
        return res.status(404).json({ error: 'Not found', details: `Form '${req.params.formId}' not found` });
      }
      res.status(500).json({
        error: 'Failed to fetch form insights',
        details: error.response?.data?.message || error.message,
        data: error.response?.data,
      });
    }
  });

  return router;
}
