/**
 * API Route: Get plant journey
 * GET /api/transport/journey/[plantId]
 */

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 { plantId } = req.query;

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

    const transportChain = getTransportChain();
    const journey = transportChain.getPlantJourney(plantId);

    if (!journey) {
      return res.status(404).json({
        success: false,
        error: `No journey found for plant: ${plantId}`
      });
    }

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