/**
 * API Route: Get environmental impact for a user
 * GET /api/transport/footprint/[userId]
 */

import type { NextApiRequest, NextApiResponse } from 'next';
import { getTransportChain } from '../../../../lib/transport/tracker';

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  if (req.method !== 'GET') {
    return res.status(405).json({ success: false, error: 'Method not allowed' });
  }

  try {
    const { userId } = req.query;

    if (!userId || typeof userId !== 'string') {
      return res.status(400).json({
        success: false,
        error: 'User ID is required'
      });
    }

    const transportChain = getTransportChain();
    const impact = transportChain.getEnvironmentalImpact(userId);

    res.status(200).json({
      success: true,
      data: impact
    });
  } catch (error: any) {
    console.error('Error fetching environmental impact:', error);
    res.status(500).json({ success: false, error: error.message || 'Internal server error' });
  }
}
