/**
 * API Route: Search for plants
 * GET /api/plants/search?q=tomato&type=species
 */

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

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

  try {
    const { q, type } = req.query;

    if (!q || typeof q !== 'string') {
      return res.status(400).json({ error: 'Missing search query parameter: q' });
    }

    const blockchain = getBlockchain();
    const searchTerm = q.toLowerCase();

    // Search in blockchain
    const allPlants = Array.from(
      new Set(blockchain.chain.map(block => block.plant.id))
    ).map(id => blockchain.getPlant(id)!);

    let results = allPlants.filter(plant => {
      if (!plant) return false;

      const searchIn = [
        plant.commonName?.toLowerCase(),
        plant.scientificName?.toLowerCase(),
        plant.genus?.toLowerCase(),
        plant.family?.toLowerCase(),
        plant.owner.name?.toLowerCase(),
      ].filter(Boolean);

      return searchIn.some(field => field?.includes(searchTerm));
    });

    // Filter by type if specified
    if (type) {
      switch (type) {
        case 'species':
          results = results.filter(p =>
            p.scientificName?.toLowerCase().includes(searchTerm)
          );
          break;
        case 'owner':
          results = results.filter(p =>
            p.owner.name?.toLowerCase().includes(searchTerm)
          );
          break;
        case 'location':
          results = results.filter(
            p =>
              p.location.city?.toLowerCase().includes(searchTerm) ||
              p.location.country?.toLowerCase().includes(searchTerm)
          );
          break;
      }
    }

    // Also search plants.net if API key is available
    let plantsNetResults = [];
    if (process.env.PLANTS_NET_API_KEY) {
      const plantsNet = getPlantsNetService();
      plantsNetResults = await plantsNet.searchPlant(q);
    }

    res.status(200).json({
      success: true,
      count: results.length,
      results: results,
      plantsNetResults: plantsNetResults,
    });
  } catch (error: any) {
    console.error('Error searching plants:', error);
    res.status(500).json({ error: error.message || 'Internal server error' });
  }
}
