// Test script for enhanced analyze-trip edge function
// Run this in browser console or Node.js environment

const testAnalyzeTrip = async () => {
  const supabaseUrl = 'YOUR_SUPABASE_URL';
  const supabaseKey = 'YOUR_SUPABASE_ANON_KEY';

  // Test Case 1: High Density Trip (Should recommend tour)
  const highDensityTrip = {
    destination: 'Cappadocia',
    travelers: 2,
    interests: ['history', 'nature', 'photography'],
    days: [
      {
        date: '2024-06-15',
        places: [
          {
            name: 'Göreme Open Air Museum',
            type: 'museum',
            lat: 38.6425,
            lng: 34.8317,
            duration: '2 hours'
          },
          {
            name: 'Uchisar Castle',
            type: 'historical',
            lat: 38.6267,
            lng: 34.8050,
            duration: '1.5 hours'
          },
          {
            name: 'Pasabag Valley',
            type: 'valley',
            lat: 38.6833,
            lng: 34.8500,
            duration: '1 hour'
          },
          {
            name: 'Devrent Valley',
            type: 'valley',
            lat: 38.7000,
            lng: 34.8667,
            duration: '45 minutes'
          },
          {
            name: 'Avanos Pottery Workshop',
            type: 'cultural',
            lat: 38.7167,
            lng: 34.8500,
            duration: '1.5 hours'
          }
        ]
      },
      {
        date: '2024-06-16',
        places: [
          {
            name: 'Derinkuyu Underground City',
            type: 'underground_city',
            lat: 38.3733,
            lng: 34.7350,
            duration: '2 hours'
          },
          {
            name: 'Ihlara Valley',
            type: 'valley',
            lat: 38.2500,
            lng: 34.3000,
            duration: '3 hours'
          },
          {
            name: 'Selime Monastery',
            type: 'historical',
            lat: 38.2833,
            lng: 34.2500,
            duration: '1 hour'
          }
        ]
      }
    ]
  };

  // Test Case 2: Low Density Trip (Should NOT recommend tour)
  const lowDensityTrip = {
    destination: 'Cappadocia',
    travelers: 2,
    interests: ['relaxation'],
    days: [
      {
        date: '2024-06-15',
        places: [
          {
            name: 'Göreme Panorama',
            type: 'panorama',
            lat: 38.6425,
            lng: 34.8317,
            duration: '1 hour'
          },
          {
            name: 'Local Cafe',
            type: 'restaurant',
            lat: 38.6400,
            lng: 34.8300,
            duration: '1.5 hours'
          }
        ]
      }
    ]
  };

  console.log('🧪 Testing High Density Trip...');
  try {
    const response1 = await fetch(`${supabaseUrl}/functions/v1/analyze-trip`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${supabaseKey}`
      },
      body: JSON.stringify(highDensityTrip)
    });

    const result1 = await response1.json();
    console.log('✅ High Density Trip Result:');
    console.log('Recommend:', result1.recommend);
    console.log('Confidence:', result1.confidence);
    console.log('Recommended Type:', result1.recommended_type);
    console.log('Daily Tour Slug:', result1.daily_tour_slug);
    console.log('\n📊 Debug Info:');
    console.log('Overall Metrics:', result1.debug_info?.overallMetrics);
    console.log('Decision Factors:', result1.debug_info?.decisionFactors);
    console.log('Reasoning:', result1.debug_info?.recommendation_reasoning);
    console.log('\n📅 Daily Metrics:');
    result1.debug_info?.dailyMetrics.forEach(day => {
      console.log(`Day ${day.dayNumber}: ${day.totalPlaces} places, ${day.totalDistanceKm}km, ${day.densityScore} density (${day.densityLevel})`);
    });
  } catch (error) {
    console.error('❌ Error testing high density trip:', error);
  }

  console.log('\n\n🧪 Testing Low Density Trip...');
  try {
    const response2 = await fetch(`${supabaseUrl}/functions/v1/analyze-trip`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${supabaseKey}`
      },
      body: JSON.stringify(lowDensityTrip)
    });

    const result2 = await response2.json();
    console.log('✅ Low Density Trip Result:');
    console.log('Recommend:', result2.recommend);
    console.log('Confidence:', result2.confidence);
    console.log('\n📊 Debug Info:');
    console.log('Overall Metrics:', result2.debug_info?.overallMetrics);
    console.log('Decision Factors:', result2.debug_info?.decisionFactors);
    console.log('Reasoning:', result2.debug_info?.recommendation_reasoning);
  } catch (error) {
    console.error('❌ Error testing low density trip:', error);
  }
};

// Run tests
testAnalyzeTrip();

/* Expected Results:

High Density Trip:
- recommend: true
- confidence: 0.75-0.90
- recommended_type: 'daily_tour'
- daily_tour_slug: 'red_tour' or 'green_tour'
- maxDensityScore: 35-50 (high) or 50+ (very_high)
- totalDistanceKm: 80-120km
- Decision factors should include "High Density Day" and "Long Distance Travel"

Low Density Trip:
- recommend: false
- confidence: 0.20-0.40
- maxDensityScore: <20 (low)
- totalDistanceKm: <30km
- Decision factors should include "Low Density"

*/
