const express = require('express');
const router = express.Router();
const gorgiasService = require('../services/gorgias.service');

// Get statistics
router.post('/stats/:name', async (req, res) => {
  try {
    const { name } = req.params;
    const filters = req.body;
    
    console.log(`Fetching ${name} statistics with filters:`, filters);
    
    if (!name) {
      return res.status(400).json({
        error: 'Missing statistic name',
        details: 'The name parameter is required'
      });
    }

    const data = await gorgiasService.getStatistics(name, filters);
    
    if (!data) {
      return res.status(404).json({
        error: 'No data found',
        details: `No statistics found for ${name}`
      });
    }

    res.json({ data });
  } catch (error) {
    console.error('Statistics error:', {
      name: req.params.name,
      filters: req.body,
      error: error.message,
      stack: error.stack,
      response: error.response?.data
    });
    
    // Handle specific error cases
    if (error.response?.status === 401) {
      return res.status(401).json({
        error: 'Authentication failed',
        details: 'Invalid Gorgias API credentials'
      });
    }
    
    if (error.response?.status === 404) {
      return res.status(404).json({
        error: 'Not found',
        details: `Statistics type '${req.params.name}' 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 statistics',
      details: error.response?.data?.message || error.message,
      data: error.response?.data
    });
  }
});

// Get tickets
router.get('/tickets', async (req, res) => {
  try {
    const data = await gorgiasService.getTickets(req.query);
    res.json(data);
  } catch (error) {
    console.error('Tickets error:', {
      params: req.query,
      error: error.message,
      response: error.response?.data
    });

    if (error.response?.status === 401) {
      return res.status(401).json({
        error: 'Authentication failed',
        details: 'Invalid Gorgias API credentials'
      });
    }

    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 tickets',
      details: error.response?.data?.message || error.message,
      data: error.response?.data
    });
  }
});

// Get customer satisfaction
router.get('/satisfaction', async (req, res) => {
  try {
    const data = await gorgiasService.getCustomerSatisfaction(req.query);
    res.json(data);
  } catch (error) {
    console.error('Satisfaction error:', error);
    res.status(500).json({
      error: 'Failed to fetch customer satisfaction',
      details: error.response?.data || error.message
    });
  }
});

module.exports = router; 