/**
 * API Route: Get network statistics
 * GET /api/plants/network
 */

import type { NextApiRequest, NextApiResponse } from 'next';
import { getBlockchain } from '../../../lib/blockchain/manager';

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

  try {
    const blockchain = getBlockchain();
    const networkStats = blockchain.getNetworkStats();

    // Calculate additional metrics
    const chainLength = blockchain.chain.length;
    const isValid = blockchain.isChainValid();

    res.status(200).json({
      success: true,
      network: networkStats,
      blockchain: {
        totalBlocks: chainLength,
        isValid,
        difficulty: blockchain.difficulty,
      },
    });
  } catch (error: any) {
    console.error('Error fetching network stats:', error);
    res.status(500).json({ error: error.message || 'Internal server error' });
  }
}
