/**
 * Test script for Dashboard API endpoints
 * Run this script to test the dashboard API endpoints
 * 
 * Usage: node test_dashboard_api.js
 */

import fetch from 'node-fetch';

const BASE_URL = 'http://localhost:3000/api/surveys/project';
const TREELOGY_ID = 1; // Replace with actual treelogy_id
const AUTH_TOKEN = 'your_jwt_token_here'; // Replace with actual token

const headers = {
  'Authorization': `Bearer ${AUTH_TOKEN}`,
  'Content-Type': 'application/json'
};

async function testDashboardAPI() {
  console.log('🧪 Testing Dashboard API Endpoints...\n');

  const endpoints = [
    {
      name: 'Dashboard Cards',
      url: `${BASE_URL}/${TREELOGY_ID}/dashboard/cards`,
      description: 'Total Trees Surveyed, Total Verified, Species Count'
    },
    {
      name: 'Condition Chart',
      url: `${BASE_URL}/${TREELOGY_ID}/dashboard/charts/condition`,
      description: 'Tree condition distribution for donut chart'
    },
    {
      name: 'Remarks Chart',
      url: `${BASE_URL}/${TREELOGY_ID}/dashboard/charts/remarks`,
      description: 'Remarks distribution for chart'
    },
    {
      name: 'Ownership Chart',
      url: `${BASE_URL}/${TREELOGY_ID}/dashboard/charts/ownership`,
      description: 'Tree ownership distribution for pie chart'
    },
    {
      name: 'Species Chart',
      url: `${BASE_URL}/${TREELOGY_ID}/dashboard/charts/species`,
      description: 'Top 15 species for bar chart'
    },
    {
      name: 'Economic Importance Chart',
      url: `${BASE_URL}/${TREELOGY_ID}/dashboard/charts/economic-importance`,
      description: 'Economic importance distribution for bar chart'
    }
  ];

  for (const endpoint of endpoints) {
    try {
      console.log(`📊 Testing ${endpoint.name}...`);
      console.log(`   Description: ${endpoint.description}`);
      console.log(`   URL: ${endpoint.url}`);
      
      const response = await fetch(endpoint.url, { headers });
      const data = await response.json();
      
      if (response.ok) {
        console.log(`   ✅ Success: ${data.message}`);
        console.log(`   📈 Data: ${JSON.stringify(data.data, null, 2)}`);
      } else {
        console.log(`   ❌ Error: ${data.message}`);
        console.log(`   🔍 Details: ${data.data}`);
      }
    } catch (error) {
      console.log(`   💥 Exception: ${error.message}`);
    }
    
    console.log(''); // Empty line for readability
  }

  console.log('🏁 Dashboard API testing completed!');
}

// Run the test
testDashboardAPI().catch(console.error);
