import { NextRequest, NextResponse } from 'next/server';
import { RowDataPacket } from 'mysql2';
import pool from '../../../../lib/db/db';

// Get DeepSeek API key from environment
const DEEPSEEK_API_KEY = process.env.DEEPSEEK_API_KEY || '';

// Define token usage interface
interface TokenUsage {
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  prompt_cache_hit_tokens?: number;
}

// City proximity data for major cities (in kilometers)
const CITY_PROXIMITY_MAP: Record<string, {city: string, distance: number}[]> = {
  'Lahore': [
    {city: 'Lahore', distance: 0},
    {city: 'Kasur', distance: 55},
    {city: 'Sheikhupura', distance: 40},
    {city: 'Nankana Sahib', distance: 75},
    {city: 'Faisalabad', distance: 130},
    {city: 'Gujranwala', distance: 110},
    {city: 'Sialkot', distance: 140},
    {city: 'Gujrat', distance: 160},
    {city: 'Rawalpindi', distance: 280},
    {city: 'Islamabad', distance: 300},
    {city: 'Multan', distance: 320},
    {city: 'Sahiwal', distance: 170},
    {city: 'Okara', distance: 110},
    {city: 'Bahawalpur', distance: 430},
    {city: 'Jhang', distance: 190},
    {city: 'Chiniot', distance: 180},
    {city: 'Kamoke', distance: 45},
    {city: 'Muridke', distance: 55},
    {city: 'Hafizabad', distance: 120},
    {city: 'Mandi Bahauddin', distance: 150}
  ],
  'Karachi': [
    {city: 'Karachi', distance: 0},
    {city: 'Hyderabad', distance: 160},
    {city: 'Thatta', distance: 100},
    {city: 'Mirpur Khas', distance: 220},
    {city: 'Nawabshah', distance: 260},
    {city: 'Sukkur', distance: 430},
    {city: 'Larkana', distance: 380},
    {city: 'Khairpur', distance: 400},
    {city: 'Badin', distance: 150},
    {city: 'Dadu', distance: 300},
    {city: 'Jamshoro', distance: 170},
    {city: 'Shikarpur', distance: 450},
    {city: 'Jacobabad', distance: 480},
    {city: 'Umerkot', distance: 280},
    {city: 'Sanghar', distance: 240},
    {city: 'Tando Allahyar', distance: 200},
    {city: 'Tando Adam', distance: 190},
    {city: 'Matli', distance: 210},
    {city: 'Moro', distance: 230},
    {city: 'Kunri', distance: 250}
  ],
  'Islamabad': [
    {city: 'Islamabad', distance: 0},
    {city: 'Rawalpindi', distance: 20},
    {city: 'Taxila', distance: 35},
    {city: 'Wah Cantt', distance: 40},
    {city: 'Attock', distance: 80},
    {city: 'Hassan Abdal', distance: 50},
    {city: 'Gujar Khan', distance: 60},
    {city: 'Jhelum', distance: 120},
    {city: 'Sargodha', distance: 180},
    {city: 'Fateh Jang', distance: 70},
    {city: 'Murree', distance: 50},
    {city: 'Abbottabad', distance: 130},
    {city: 'Mansehra', distance: 150},
    {city: 'Haripur', distance: 90},
    {city: 'Talagang', distance: 140},
    {city: 'Chakwal', distance: 110},
    {city: 'Mianwali', distance: 220},
    {city: 'Kohat', distance: 170},
    {city: 'Charsadda', distance: 160},
    {city: 'Nowshera', distance: 140}
  ],
  'Delhi': [
    {city: 'Delhi', distance: 0},
    {city: 'Gurgaon', distance: 30},
    {city: 'Noida', distance: 25},
    {city: 'Faridabad', distance: 35},
    {city: 'Ghaziabad', distance: 20},
    {city: 'Meerut', distance: 70},
    {city: 'Sonipat', distance: 50},
    {city: 'Panipat', distance: 90},
    {city: 'Rohtak', distance: 80},
    {city: 'Rewari', distance: 85},
    {city: 'Jhajjar', distance: 60},
    {city: 'Bagpat', distance: 55},
    {city: 'Bulandshahr', distance: 75},
    {city: 'Aligarh', distance: 140},
    {city: 'Mathura', distance: 150},
    {city: 'Agra', distance: 200},
    {city: 'Karnal', distance: 130},
    {city: 'Kurukshetra', distance: 170},
    {city: 'Ambala', distance: 200},
    {city: 'Chandigarh', distance: 250}
  ],
  'Dubai': [
    {city: 'Dubai', distance: 0},
    {city: 'Sharjah', distance: 25},
    {city: 'Abu Dhabi', distance: 140},
    {city: 'Al Ain', distance: 130},
    {city: 'Ajman', distance: 35},
    {city: 'Ras Al Khaimah', distance: 110},
    {city: 'Fujairah', distance: 120},
    {city: 'Umm Al Quwain', distance: 50},
    {city: 'Al Dhaid', distance: 70},
    {city: 'Khor Fakkan', distance: 130},
    {city: 'Dibba', distance: 150},
    {city: 'Jebel Ali', distance: 40},
    {city: 'Hatta', distance: 115},
    {city: 'Kalba', distance: 140},
    {city: 'Madinat Zayed', distance: 200},
    {city: 'Liwa Oasis', distance: 250},
    {city: 'Ruwais', distance: 270},
    {city: 'Ghantoot', distance: 65},
    {city: 'Masafi', distance: 105},
    {city: 'Dhaid', distance: 70}
  ],
  'London': [
    {city: 'London', distance: 0},
    {city: 'Birmingham', distance: 190},
    {city: 'Manchester', distance: 320},
    {city: 'Liverpool', distance: 350},
    {city: 'Bristol', distance: 190},
    {city: 'Sheffield', distance: 270},
    {city: 'Leeds', distance: 310},
    {city: 'Leicester', distance: 160},
    {city: 'Nottingham', distance: 200},
    {city: 'Cambridge', distance: 95},
    {city: 'Oxford', distance: 90},
    {city: 'Brighton', distance: 85},
    {city: 'Southampton', distance: 130},
    {city: 'Portsmouth', distance: 120},
    {city: 'Reading', distance: 65},
    {city: 'Coventry', distance: 150},
    {city: 'Stoke-on-Trent', distance: 240},
    {city: 'Wolverhampton', distance: 200},
    {city: 'Plymouth', distance: 310},
    {city: 'Norwich', distance: 180}
  ],
  'Mumbai': [
    {city: 'Mumbai', distance: 0},
    {city: 'Thane', distance: 25},
    {city: 'Navi Mumbai', distance: 30},
    {city: 'Panvel', distance: 40},
    {city: 'Pune', distance: 150},
    {city: 'Nagpur', distance: 850},
    {city: 'Nashik', distance: 170},
    {city: 'Aurangabad', distance: 340},
    {city: 'Solapur', distance: 400},
    {city: 'Kolhapur', distance: 380},
    {city: 'Ratnagiri', distance: 330},
    {city: 'Ahmednagar', distance: 250},
    {city: 'Sangli', distance: 360},
    {city: 'Satara', distance: 240},
    {city: 'Dhule', distance: 280},
    {city: 'Jalgaon', distance: 420},
    {city: 'Amravati', distance: 660},
    {city: 'Akola', distance: 580},
    {city: 'Latur', distance: 500},
    {city: 'Nanded', distance: 560}
  ]
};

// Function to extract radius and city from query - IMPROVED
function extractRadiusAndCity(query: string): { radiusKm: number; city: string | null; cleanQuery: string } {
  console.log('🔍 Parsing query for radius:', query);
  
  let radiusKm = 0;
  let city = null;
  let cleanQuery = query;
  
  // Check if there's a radius parameter in the URL
  const urlParams = new URLSearchParams(query.includes('?') ? query.split('?')[1] : '');
  const urlRadius = urlParams.get('radius_km');
  if (urlRadius && !isNaN(parseInt(urlRadius))) {
    radiusKm = parseInt(urlRadius);
    console.log(`📍 Radius from URL parameter: ${radiusKm}km`);
  }
  
  // Extract [RADIUS:100km CITY:Lahore] pattern
  const radiusPattern = /\[RADIUS:(\d+)km\s*(?:CITY:([^\]]+))?\]/i;
  const radiusMatch = query.match(radiusPattern);
  
  if (radiusMatch) {
    radiusKm = parseInt(radiusMatch[1]);
    city = radiusMatch[2]?.trim() || null;
    cleanQuery = query.replace(radiusPattern, '').trim();
    console.log(`📍 Extracted from pattern: radius=${radiusKm}km, city=${city}`);
  }
  
  // Extract [EXACT_CITY:Lahore] pattern
  const exactCityPattern = /\[EXACT_CITY:([^\]]+)\]/i;
  const exactCityMatch = query.match(exactCityPattern);
  if (exactCityMatch) {
    city = exactCityMatch[1].trim();
    radiusKm = 0; // Exact city only
    cleanQuery = query.replace(exactCityPattern, '').trim();
    console.log(`📍 Extracted exact city: ${city}`);
  }
  
  // If no pattern found, try to extract from query text
  if (!radiusMatch && !exactCityMatch) {
    // Look for radius in text
    const textRadiusMatch = query.match(/(?:within|radius|around|near|nearby)\s+(\d+)\s*(?:km|kilometers?)?/i);
    if (textRadiusMatch) {
      radiusKm = parseInt(textRadiusMatch[1]);
      console.log(`📍 Extracted radius from text: ${radiusKm}km`);
    } else if (query.toLowerCase().includes('nearby') || query.toLowerCase().includes('around') || query.toLowerCase().includes('near')) {
      radiusKm = 100; // Default radius for "nearby"
      console.log(`📍 Default radius for "nearby": ${radiusKm}km`);
    }
    
    // Look for city in text - check all known cities
    const cities = Object.keys(CITY_PROXIMITY_MAP);
    const queryLower = query.toLowerCase();
    
    for (const cityName of cities) {
      if (queryLower.includes(cityName.toLowerCase())) {
        city = cityName;
        console.log(`📍 Extracted city from text: ${city}`);
        break;
      }
    }
    
    // If no city found in known cities, try to extract any city-like pattern
    if (!city) {
      const cityPattern = /(?:from|in|near|around|at)\s+([A-Z][a-zA-Z\s]+?)(?:\.|,|$)/i;
      const cityMatch = query.match(cityPattern);
      if (cityMatch) {
        const potentialCity = cityMatch[1].trim();
        // Basic validation - at least 2 chars and doesn't contain common non-city words
        if (potentialCity.length >= 2 && 
            !potentialCity.toLowerCase().includes('need') &&
            !potentialCity.toLowerCase().includes('help') &&
            !potentialCity.toLowerCase().includes('consultant') &&
            !potentialCity.toLowerCase().includes('visa')) {
          city = potentialCity;
          console.log(`📍 Extracted potential city: ${city}`);
        }
      }
    }
  }
  
  console.log(`📊 Final extraction: radius=${radiusKm}km, city=${city}, cleanQuery="${cleanQuery}"`);
  return { radiusKm, city, cleanQuery };
}

// Function to call DeepSeek AI for filter extraction - SIMPLIFIED
async function extractFiltersWithAI(query: string): Promise<any> {
  const aiStartTime = Date.now();
  
  // First extract radius and city from query
  const { radiusKm, city, cleanQuery } = extractRadiusAndCity(query);
  
  if (!DEEPSEEK_API_KEY) {
    console.error('❌ DeepSeek API key not configured');
    return extractFiltersManually(cleanQuery, radiusKm, city);
  }

  try {
    // Build a simpler prompt that doesn't confuse the AI
    const prompt = `Extract immigration consultant search parameters from this query: "${cleanQuery}"
    
Extracted context:
${city ? `- Mentioned city: ${city}` : '- No city mentioned'}
${radiusKm > 0 ? `- Search radius: ${radiusKm}km` : '- Exact city search (no radius)'}

Return ONLY valid JSON with this exact structure:
{
  "filters": {
    "target_countries": ["list of countries mentioned"],
    "specialization": ["list of visa types mentioned"],
    "city": "${city || 'null'}",
    "radius_km": ${radiusKm},
    "response_time": "urgent/fast/normal",
    "digital_only": true/false,
    "max_price": null,
    "languages": [],
    "query_summary": "brief summary"
  },
  "confidence": 0.0-1.0
}`;

    console.log('🤖 Sending prompt to AI...');
    
    const response = await fetch('https://api.deepseek.com/v1/chat/completions', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${DEEPSEEK_API_KEY}`
      },
      body: JSON.stringify({
        model: 'deepseek-chat',
        messages: [
          {
            role: 'system',
            content: 'You are a precise filter extraction assistant. Extract only mentioned parameters. If something is not mentioned, leave it null/empty. Return ONLY valid JSON.'
          },
          {
            role: 'user',
            content: prompt
          }
        ],
        temperature: 0.1,
        max_tokens: 500
      })
    });

    const aiResponseTime = Date.now() - aiStartTime;

    if (!response.ok) {
      const errorData = await response.text();
      console.error('❌ DeepSeek API error:', response.status, errorData);
      return extractFiltersManually(cleanQuery, radiusKm, city);
    }

    const data = await response.json();
    const aiResponse = data.choices[0]?.message?.content;
    const tokenUsage: TokenUsage = data.usage || {
      prompt_tokens: 0,
      completion_tokens: 0,
      total_tokens: 0
    };
    
    console.log('🤖 AI Raw Response:', aiResponse.substring(0, 200) + '...');
    console.log('   ⏱️  Response Time:', aiResponseTime, 'ms');
    console.log('   🧮 Token Usage:', tokenUsage);
    
    // Try to find JSON in the response
    const jsonMatch = aiResponse.match(/\{[\s\S]*\}/);
    if (!jsonMatch) {
      console.error('❌ Failed to find JSON in AI response');
      return extractFiltersManually(cleanQuery, radiusKm, city);
    }
    
    try {
      const parsedResponse = JSON.parse(jsonMatch[0]);
      
      // Get nearby cities if radius is specified
      let nearbyCities: string[] = [];
      let allSearchCities: string[] = [];
      
      const mainCity = parsedResponse.filters.city && parsedResponse.filters.city !== 'null' 
        ? parsedResponse.filters.city 
        : city;
      
      const finalRadiusKm = parsedResponse.filters.radius_km || radiusKm;
      
      if (mainCity) {
        // Always include main city
        allSearchCities.push(mainCity);
        
        if (finalRadiusKm > 0 && CITY_PROXIMITY_MAP[mainCity]) {
          nearbyCities = CITY_PROXIMITY_MAP[mainCity]
            .filter(cityData => cityData.distance <= finalRadiusKm && cityData.distance > 0)
            .map(cityData => cityData.city)
            .slice(0, 20);
          
          allSearchCities.push(...nearbyCities);
          
          console.log(`📍 Nearby cities for ${mainCity} (${finalRadiusKm}km radius):`, nearbyCities);
        }
        
        // Update filters with actual values
        parsedResponse.filters.city = mainCity;
        parsedResponse.filters.radius_km = finalRadiusKm;
      }
      
      return {
        ...parsedResponse,
        nearby_cities: nearbyCities,
        search_cities: allSearchCities,
        token_usage: tokenUsage,
        processing_time_ms: aiResponseTime,
        ai_used: true,
        api_model_used: 'deepseek-chat',
        query: cleanQuery
      };
      
    } catch (parseError) {
      console.error('❌ Failed to parse AI JSON response:', parseError);
      return extractFiltersManually(cleanQuery, radiusKm, city);
    }
    
  } catch (error: any) {
    console.error('❌ AI Filter extraction error:', error);
    return {
      filters: extractFiltersManually(cleanQuery, radiusKm, city).filters,
      confidence: 0.3,
      extracted_terms: [],
      token_usage: {
        prompt_tokens: 0,
        completion_tokens: 0,
        total_tokens: 0
      },
      processing_time_ms: Date.now() - aiStartTime,
      ai_used: false,
      error: error.message,
      query: cleanQuery
    };
  }
}

// Manual extraction - IMPROVED
function extractFiltersManually(query: string, radiusKm: number = 0, city: string | null = null): any {
  const queryLower = query.toLowerCase();
  
  const filters = {
    target_countries: [] as string[],
    specialization: [] as string[],
    city: city,
    radius_km: radiusKm,
    services: [] as string[],
    response_time: 'normal' as string,
    digital_only: false,
    max_price: null as number | null,
    languages: [] as string[],
    query_summary: query,
  };

  // Country extraction
  if (queryLower.includes('germany')) filters.target_countries.push('Germany');
  if (queryLower.includes('canada')) filters.target_countries.push('Canada');
  if (queryLower.includes('usa') || queryLower.includes('united states') || queryLower.includes('america')) filters.target_countries.push('USA');
  if (queryLower.includes('uk') || queryLower.includes('united kingdom') || queryLower.includes('britain')) filters.target_countries.push('UK');
  if (queryLower.includes('australia')) filters.target_countries.push('Australia');
  if (queryLower.includes('uae') || queryLower.includes('dubai') || queryLower.includes('emirates')) filters.target_countries.push('UAE');
  if (queryLower.includes('singapore')) filters.target_countries.push('Singapore');
  if (queryLower.includes('new zealand') || queryLower.includes('nz')) filters.target_countries.push('New Zealand');
  
  // Specialization extraction
  if (queryLower.includes('student')) filters.specialization.push('Student Visa');
  if (queryLower.includes('work') || queryLower.includes('employment')) filters.specialization.push('Work Visa');
  if (queryLower.includes('business')) filters.specialization.push('Business Visa');
  if (queryLower.includes('tour') || queryLower.includes('visitor')) filters.specialization.push('Tourist Visa');
  if (queryLower.includes('family')) filters.specialization.push('Family Visa');
  if (queryLower.includes('pr') || queryLower.includes('permanent residency')) filters.specialization.push('Permanent Residency');
  
  // City extraction if not already provided
  if (!filters.city) {
    const cityKeywords = Object.keys(CITY_PROXIMITY_MAP);
    for (const city of cityKeywords) {
      if (queryLower.includes(city.toLowerCase())) {
        filters.city = city;
        break;
      }
    }
  }
  
  // Radius extraction if not already provided
  if (!filters.radius_km) {
    const radiusMatch = queryLower.match(/(?:within|radius|around|near|nearby)\s+(\d+)\s*(?:km|kilometers?)?/i);
    if (radiusMatch) {
      filters.radius_km = parseInt(radiusMatch[1]);
    } else if (queryLower.includes('nearby') || queryLower.includes('around') || queryLower.includes('near')) {
      filters.radius_km = 100;
    }
  }
  
  // Response time
  if (queryLower.includes('fast') || queryLower.includes('quick') || queryLower.includes('asap')) filters.response_time = 'fast';
  if (queryLower.includes('urgent') || queryLower.includes('emergency') || queryLower.includes('immediate')) filters.response_time = 'urgent';
  
  // Digital only
  if (queryLower.includes('online') || queryLower.includes('digital') || queryLower.includes('virtual') || queryLower.includes('remote')) filters.digital_only = true;
  
  // Price extraction
  const priceMatch = query.match(/\$?(\d+)\s*(?:dollars?|usd)?/i);
  if (priceMatch) {
    filters.max_price = parseInt(priceMatch[1]);
  }
  
  // Language extraction
  if (queryLower.includes('english')) filters.languages.push('English');
  if (queryLower.includes('urdu')) filters.languages.push('Urdu');
  if (queryLower.includes('hindi')) filters.languages.push('Hindi');
  if (queryLower.includes('arabic')) filters.languages.push('Arabic');
  
  // Get nearby cities if we have a city and radius
  let nearbyCities: string[] = [];
  let searchCities: string[] = [];
  
  if (filters.city && filters.radius_km > 0 && CITY_PROXIMITY_MAP[filters.city]) {
    searchCities.push(filters.city);
    nearbyCities = CITY_PROXIMITY_MAP[filters.city]
      .filter(cityData => cityData.distance <= filters.radius_km && cityData.distance > 0)
      .map(cityData => cityData.city)
      .slice(0, 20);
    
    searchCities.push(...nearbyCities);
  } else if (filters.city) {
    searchCities.push(filters.city);
  }
  
  return { 
    filters, 
    confidence: 0.8, 
    extracted_terms: [],
    token_usage: {
      prompt_tokens: 0,
      completion_tokens: 0,
      total_tokens: 0
    },
    processing_time_ms: 0,
    ai_used: false,
    nearby_cities: nearbyCities,
    search_cities: searchCities,
    query: query
  };
}

// Parse JSON fields
function safeParseJSON(value: any): any[] {
  if (!value || value === 'NULL' || value === 'null') return [];
  
  try {
    if (typeof value === 'string') {
      const trimmed = value.trim();
      if (!trimmed || trimmed === '[]' || trimmed === '""') return [];
      
      if (trimmed.startsWith('[') && trimmed.endsWith(']')) {
        try {
          return JSON.parse(trimmed);
        } catch {
          // Try to extract values
          const matches = trimmed.match(/"([^"]+)"/g);
          return matches ? matches.map(m => m.replace(/"/g, '')) : [];
        }
      }
      
      return [trimmed];
    }
    
    return Array.isArray(value) ? value : [value];
  } catch {
    return [];
  }
}

// Calculate match score
function calculateAIMatchScore(consultant: any, aiFilters: any, searchCities: string[]): number {
  let score = 0;
  
  // 1. Country match (40 points)
  if (aiFilters.target_countries?.length > 0) {
    const consultantCountries = safeParseJSON(consultant.target_countries);
    const consultantCountriesLower = consultantCountries.map((c: string) => c.toLowerCase());
    
    for (const filterCountry of aiFilters.target_countries) {
      const filterCountryLower = filterCountry.toLowerCase();
      for (const consultantCountry of consultantCountriesLower) {
        if (consultantCountry.includes(filterCountryLower) || 
            filterCountryLower.includes(consultantCountry)) {
          score += 40;
          break;
        }
      }
    }
  }

  // 2. Specialization match (30 points)
  if (aiFilters.specialization?.length > 0) {
    const consultantSpecs = safeParseJSON(consultant.specialization);
    const consultantSpecsLower = consultantSpecs.map((s: string) => s.toLowerCase());
    
    for (const filterSpec of aiFilters.specialization) {
      const filterSpecLower = filterSpec.toLowerCase();
      for (const consultantSpec of consultantSpecsLower) {
        if (consultantSpec.includes(filterSpecLower) || 
            filterSpecLower.includes(consultantSpec)) {
          score += 30;
          break;
        }
      }
    }
  }

  // 3. City match (30 points) - Check against all search cities
  if (searchCities.length > 0 && consultant.city) {
    const consultantCityLower = consultant.city.toString().toLowerCase();
    
    for (const searchCity of searchCities) {
      const searchCityLower = searchCity.toLowerCase();
      if (consultantCityLower === searchCityLower || 
          consultantCityLower.includes(searchCityLower) || 
          searchCityLower.includes(consultantCityLower)) {
        score += 30;
        break;
      }
    }
  }

  // 4. Response time match (5 points) - Flexible
  if (aiFilters.response_time && consultant.response_time) {
    const consultantResponse = consultant.response_time.toLowerCase();
    const filterResponse = aiFilters.response_time.toLowerCase();
    
    if (consultantResponse === filterResponse) {
      score += 5;
    } else if ((filterResponse === 'urgent' && consultantResponse === 'fast') ||
               (filterResponse === 'fast' && consultantResponse === 'urgent')) {
      score += 3;
    }
  }

  // 5. Digital only match (5 points)
  if (aiFilters.digital_only && consultant.digital_only) {
    score += 5;
  }

  // 6. Price match (bonus if within budget)
  if (aiFilters.max_price && consultant.consultation_fee) {
    if (consultant.consultation_fee <= aiFilters.max_price) {
      score += 10;
    } else if (consultant.consultation_fee <= aiFilters.max_price * 1.5) {
      score += 5;
    }
  }

  return Math.min(100, score);
}

// Main API Handler - SIMPLIFIED
export async function GET(request: NextRequest) {


const comingHeaders = request.headers.get("authorization");

if(comingHeaders !== process.env.APP_API_KEY) {
  return NextResponse.json(
    { success: false, message: 'Unauthorized Access' },
    { status: 401 }
  );
}

  const requestStartTime = Date.now();
  const searchParams = request.nextUrl.searchParams;
  const connection = await pool.getConnection();

  try {
    const country = searchParams.get('country');
    const city = searchParams.get('city');
    const exactMatch = searchParams.get('exact_match') === 'true';
    const digitalOnly = searchParams.get('digital_only') === 'true';
    const aiQuery = searchParams.get('ai_query');
    const radiusParam = searchParams.get('radius_km');
    
    // Track total API metrics
    const apiMetrics = {
      ai_used: false,
      token_usage: null as TokenUsage | null,
      processing_time_ms: 0,
      total_processing_time_ms: 0,
      ai_model: null as string | null,
      cache_hit_tokens: null as number | null
    };

    console.log('🔍 Search started with:', { 
      country, 
      city, 
      exactMatch, 
      digitalOnly, 
      aiQuery: aiQuery?.substring(0, 100),
      radiusParam 
    });

    // AI Search Mode
    if (aiQuery) {
      console.log('🤖 AI Search mode');
      const aiStartTime = Date.now();
      
      // If radius param is provided, append it to the query
      let finalQuery = aiQuery;
      if (radiusParam) {
        finalQuery = `${aiQuery} [RADIUS:${radiusParam}km]`;
        console.log(`📍 Added radius from param: ${radiusParam}km`);
      }
      
      const aiResult = await extractFiltersWithAI(finalQuery);
      const aiFilters = aiResult.filters;
      const searchCities = aiResult.search_cities || [];
      
      // Update API metrics
      apiMetrics.ai_used = aiResult.ai_used || false;
      apiMetrics.token_usage = aiResult.token_usage || null;
      apiMetrics.processing_time_ms = aiResult.processing_time_ms || 0;
      apiMetrics.total_processing_time_ms = Date.now() - aiStartTime;
      apiMetrics.ai_model = aiResult.api_model_used || null;
      apiMetrics.cache_hit_tokens = aiResult.token_usage?.prompt_cache_hit_tokens || null;
      
      console.log('🎯 AI Filters:', JSON.stringify(aiFilters, null, 2));
      console.log('📍 Search cities:', searchCities);
      console.log('📍 Nearby cities:', aiResult.nearby_cities);
      console.log('📊 AI Confidence:', aiResult.confidence);
      
      let query = `
        SELECT 
          c.*,
          COALESCE(c.average_rating, 0) as average_rating,
          COALESCE(c.rating_count, 0) as rating_count,
          COALESCE(c.consultation_fee, 0) as consultation_fee
        FROM consultants c
        WHERE c.active = '1' AND c.active_profile = '1' AND c.profile_status = 'approved'
      `;

      const queryParams: any[] = [];

      // Build WHERE conditions
      const conditions: string[] = [];

      // Country condition
      if (aiFilters.target_countries?.length > 0) {
        const countryConditions = aiFilters.target_countries.map(() => 
          `c.target_countries LIKE ?`
        );
        conditions.push(`(${countryConditions.join(' OR ')})`);
        aiFilters.target_countries.forEach((country: string) => {
          queryParams.push(`%${country}%`);
        });
      }

      // City condition - Search in all relevant cities
      if (searchCities.length > 0) {
        const cityConditions = searchCities.map(() => 
          `LOWER(c.city) LIKE LOWER(?)`
        );
        conditions.push(`(${cityConditions.join(' OR ')})`);
        searchCities.forEach((city: string) => {
          queryParams.push(`%${city}%`);
        });
      } else if (aiFilters.city && aiFilters.city !== 'null') {
        // If no search cities but we have a city filter, use that
        conditions.push(`LOWER(c.city) LIKE LOWER(?)`);
        queryParams.push(`%${aiFilters.city}%`);
      }

      // Digital only
      if (aiFilters.digital_only) {
        conditions.push(`c.digital_only = 1`);
      }

      // Add conditions to query
      if (conditions.length > 0) {
        query += ` AND ${conditions.join(' AND ')}`;
      }

      query += ` ORDER BY c.id DESC LIMIT 100`;

      console.log('📝 SQL Query:', query);
      console.log('🔢 Query Params:', queryParams);

      const [consultants] = await connection.query<RowDataPacket[]>(query, queryParams);
      console.log(`✅ Found ${consultants.length} raw consultants`);

      // Calculate scores with all search cities
      const scoredConsultants = consultants.map(consultant => {
        const aiMatchScore = calculateAIMatchScore(consultant, aiFilters, searchCities);
        
        // Determine which city matched
        let matchedCity = null;
        if (consultant.city && searchCities.length > 0) {
          const consultantCityLower = consultant.city.toString().toLowerCase();
          for (const searchCity of searchCities) {
            const searchCityLower = searchCity.toLowerCase();
            if (consultantCityLower.includes(searchCityLower) || searchCityLower.includes(consultantCityLower)) {
              matchedCity = searchCity;
              break;
            }
          }
        }
        
        return {
          id: consultant.id,
          name: consultant.name,
          company_name: consultant.company_name,
          slug: consultant.seo_slug,
          phone: consultant.phone,
          city: consultant.city,
          average_rating: parseFloat(consultant.average_rating) || 0,
          rating_count: parseInt(consultant.rating_count) || 0,
          consultation_fee: parseFloat(consultant.consultation_fee) || 0,
          services: safeParseJSON(consultant.services),
          specialization: safeParseJSON(consultant.specialization),
          target_countries: safeParseJSON(consultant.target_countries),
          languages: safeParseJSON(consultant.languages),
          response_time: consultant.response_time || 'normal',
          digital_only: Boolean(consultant.digital_only),
          profile_picture: consultant.profile_picture,
          ai_match_score: aiMatchScore,
          ai_match_reason: aiMatchScore > 0 ? 
            (matchedCity ? `Located in ${matchedCity}${searchCities.length > 1 ? ` (within ${aiFilters.radius_km || 0}km radius)` : ''}` : 'Matches your criteria') 
            : 'Basic match',
          matched_city: matchedCity,
          ranking_position: null
        };
      });

      // Filter out zero scores and sort
      const filteredConsultants = scoredConsultants.filter(c => c.ai_match_score > 0);
      filteredConsultants.sort((a, b) => b.ai_match_score - a.ai_match_score);
      
      // Add ranking
      const rankedConsultants = filteredConsultants.map((consultant, index) => ({
        ...consultant,
        ranking_position: index + 1
      }));

      console.log(`📊 After filtering: ${rankedConsultants.length} consultants with score > 0`);
      
      // Create summary
      let summary = '';
      if (rankedConsultants.length > 0) {
        if (aiFilters.city && aiFilters.city !== 'null') {
          if (aiFilters.radius_km > 0) {
            summary = `Found ${rankedConsultants.length} consultant${rankedConsultants.length !== 1 ? 's' : ''} within ${aiFilters.radius_km}km of ${aiFilters.city}`;
          } else {
            summary = `Found ${rankedConsultants.length} consultant${rankedConsultants.length !== 1 ? 's' : ''} in ${aiFilters.city}`;
          }
        } else {
          summary = `Found ${rankedConsultants.length} consultant${rankedConsultants.length !== 1 ? 's' : ''} matching your criteria`;
        }
      } else {
        summary = 'No consultants found matching your criteria';
      }
      
      return NextResponse.json({
        success: true,
        data: rankedConsultants,
        filters: aiFilters,
        nearby_cities: aiResult.nearby_cities || [],
        search_cities: searchCities,
        search_radius_km: aiFilters.radius_km || 0,
        ai_confidence: aiResult.confidence || 0,
        ai_metrics: {
          ai_used: apiMetrics.ai_used,
          token_usage: apiMetrics.token_usage,
          processing_time_ms: apiMetrics.processing_time_ms,
          total_processing_time_ms: apiMetrics.total_processing_time_ms,
          ai_model: apiMetrics.ai_model,
          cache_hit_tokens: apiMetrics.cache_hit_tokens
        },
        summary: summary,
        count: rankedConsultants.length,
        message: 'AI search completed',
        request_time_ms: Date.now() - requestStartTime,
        ai_query: aiResult.query || aiQuery
      });

    } else {
      // Standard (Manual) Search Mode with country OR city logic
      console.log('🔧 Standard Search mode');
      let query = `
        SELECT 
          c.*,
          COALESCE(c.average_rating, 0) as average_rating,
          COALESCE(c.rating_count, 0) as rating_count,
          COALESCE(c.consultation_fee, 0) as consultation_fee
        FROM consultants c
        WHERE c.active = '1' AND c.active_profile = '1' AND c.profile_status = 'approved'
      `;

      const conditions: string[] = [];
      const queryParams: any[] = [];

      // Combined country OR city condition for manual search
      const locationConditions: string[] = [];

      // Add country condition if provided
      if (country) {
        locationConditions.push(`(c.target_countries LIKE ? OR c.target_countries IS NULL)`);
        queryParams.push(`%${country}%`);
      }
      
      // Add city condition if provided
      if (city) {
        if (exactMatch) {
          locationConditions.push(`LOWER(c.city) = LOWER(?)`);
        } else {
          locationConditions.push(`LOWER(c.city) LIKE LOWER(?)`);
        }
        queryParams.push(exactMatch ? city : `%${city}%`);
      }
      
      // If we have any location conditions, combine them with OR
      if (locationConditions.length > 0) {
        conditions.push(`(${locationConditions.join(' OR ')})`);
      }
      
      // Digital only filter (AND with location conditions)
      if (digitalOnly) {
        conditions.push(`c.digital_only = 1`);
      }

      if (conditions.length > 0) {
        query += ` AND ${conditions.join(' AND ')}`;
      }

      query += ` ORDER BY c.id DESC LIMIT 50`;

      console.log('📝 Standard SQL Query:', query);
      console.log('🔢 Standard Query Params:', queryParams);

      const [consultants] = await connection.query<RowDataPacket[]>(query, queryParams);
      console.log(`✅ Found ${consultants.length} consultants in standard search`);

      const formattedConsultants = consultants.map(consultant => {
        // Determine match reason for manual search
        let matchReason = 'Standard search result';
        let matchedCountry = null;
        let matchedCity = null;
        
        // Check if matched by country
        if (country && consultant.target_countries) {
          const targetCountries = safeParseJSON(consultant.target_countries) || [];
          if (targetCountries.some((tc: string) => 
            tc.toLowerCase().includes(country.toLowerCase()) || 
            country.toLowerCase().includes(tc.toLowerCase())
          )) {
            matchedCountry = country;
          }
        }
        
        // Check if matched by city
        if (city && consultant.city) {
          const consultantCityLower = consultant.city.toString().toLowerCase();
          const searchCityLower = city.toLowerCase();
          if (exactMatch) {
            if (consultantCityLower === searchCityLower) {
              matchedCity = city;
            }
          } else {
            if (consultantCityLower.includes(searchCityLower) || searchCityLower.includes(consultantCityLower)) {
              matchedCity = city;
            }
          }
        }
        
        // Update match reason based on what was matched
        if (matchedCity && matchedCountry) {
          matchReason = `Located in ${matchedCity} and serves ${matchedCountry}`;
        } else if (matchedCity) {
          matchReason = `Located in ${matchedCity}`;
        } else if (matchedCountry) {
          matchReason = `Serves clients in ${matchedCountry}`;
        }
        
        return {
          id: consultant.id,
          name: consultant.name,
          company_name: consultant.company_name,
          slug: consultant.seo_slug,
          phone: consultant.phone,
          city: consultant.city,
          average_rating: parseFloat(consultant.average_rating) || 0,
          rating_count: parseInt(consultant.rating_count) || 0,
          consultation_fee: parseFloat(consultant.consultation_fee) || 0,
          services: safeParseJSON(consultant.services),
          specialization: safeParseJSON(consultant.specialization),
          target_countries: safeParseJSON(consultant.target_countries),
          languages: safeParseJSON(consultant.languages),
          response_time: consultant.response_time || 'normal',
          digital_only: Boolean(consultant.digital_only),
          profile_picture: consultant.profile_picture,
          ai_match_score: 0,
          ai_match_reason: matchReason,
          matched_city: matchedCity,
          matched_country: matchedCountry,
          ranking_position: null
        };
      });

      return NextResponse.json({
        success: true,
        data: formattedConsultants,
        count: formattedConsultants.length,
        message: 'Standard search completed',
        ai_metrics: {
          ai_used: false,
          token_usage: null,
          processing_time_ms: 0,
          total_processing_time_ms: 0
        },
        summary: formattedConsultants.length > 0 
          ? `Found ${formattedConsultants.length} consultant${formattedConsultants.length !== 1 ? 's' : ''}${country ? ` serving ${country}` : ''}${city ? ` in ${city}` : ''}`
          : 'No consultants found matching your criteria',
        request_time_ms: Date.now() - requestStartTime
      });
    }

  } catch (err: any) {
    console.error('❌ API Error:', err);
    return NextResponse.json(
      {
        success: false,
        message: err.message || 'Internal server error',
        data: [],
        ai_metrics: {
          ai_used: false,
          token_usage: null,
          processing_time_ms: 0,
          total_processing_time_ms: 0,
          error: err.message
        }
      },
      { status: 500 }
    );
  } finally {
    connection.release();
    console.log('⏱️  Total Request Time:', Date.now() - requestStartTime, 'ms');
  }
}