/**
 * API Route: Get farm details
 * GET /api/vertical-farm/[farmId]
 */

import type { NextApiRequest, NextApiResponse } from 'next';
import { getVerticalFarmController } from '../../../../lib/vertical-farming/controller';

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

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

    const controller = getVerticalFarmController();
    const farm = controller.getFarm(farmId);

    if (!farm) {
      return res.status(404).json({
        success: false,
        error: `Farm not found: ${farmId}`
      });
    }

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