// Quick test to fetch Auvik device configuration
const deviceId = 'NTAyNzUxNTczODUzNjc1MjYxLDExNDk1OTYxNzUxOTczNTA4MjU';
const tenantId = '502751573853675261'; // hynesindustries tenant
const apiUrl = process.env.AUVIK_API_URL;
const apiUser = process.env.AUVIK_API_USER;
const apiKey = process.env.AUVIK_API_KEY;

const credentials = Buffer.from(`${apiUser}:${apiKey}`).toString('base64');

// Try configuration endpoints with proper tenant parameter
const endpoints = [
  { name: 'Configuration with tenant and deviceId filter', url: `${apiUrl}/v1/inventory/configuration?tenants=${tenantId}&filter[deviceId]=${deviceId}` },
  { name: 'Configuration with tenant only', url: `${apiUrl}/v1/inventory/configuration?tenants=${tenantId}` },
  { name: 'Single Configuration', url: `${apiUrl}/v1/inventory/configuration/${deviceId}?tenants=${tenantId}` },
];

console.log('Testing Auvik API endpoints for device:', deviceId);
console.log('Device: YNGHYNSWP19 (Serial: TW35L3R0FS)\n');

async function testEndpoint(endpoint) {
  console.log(`\n=== Testing: ${endpoint.name} ===`);
  console.log('URL:', endpoint.url);
  
  try {
    const response = await fetch(endpoint.url, {
      headers: {
        'Authorization': `Basic ${credentials}`,
        'Accept': 'application/json',
      }
    });
    
    console.log('Status:', response.status, response.statusText);
    const text = await response.text();
    
    if (!response.ok) {
      console.error('Error:', text.substring(0, 200));
      return;
    }
    
    try {
      const data = JSON.parse(text);
      console.log('✓ Success! Response:');
      console.log(JSON.stringify(data, null, 2).substring(0, 2000));
      
      // Special handling for configuration
      if (endpoint.name === 'Configuration' && data.data && data.data.length > 0) {
        const latest = data.data[0];
        console.log('\n=== Latest Configuration Details ===');
        console.log('Type:', latest.attributes.configType);
        console.log('Backup Date:', latest.attributes.backupDate);
        console.log('Size:', latest.attributes.configSize, 'bytes');
        
        if (latest.attributes.configText) {
          console.log('\n=== Configuration Text (first 1000 chars) ===');
          console.log(latest.attributes.configText.substring(0, 1000));
        }
      }
    } catch (e) {
      console.log('Response (not JSON):', text.substring(0, 500));
    }
  } catch (error) {
    console.error('Fetch error:', error.message);
  }
}

async function runTests() {
  for (const endpoint of endpoints) {
    await testEndpoint(endpoint);
  }
}

runTests();
