// Fetch the actual configuration text for YNGHYNSWP19
const configId = 'NTAyNzUxNTczODUzNjc1MjYxLDExNDk1OTYxNjAyMTY0NDM4MTc';
const tenantId = '502751573853675261';
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');

console.log('Fetching configuration text for YNGHYNSWP19');
console.log('Config ID:', configId);
console.log('Tenant ID:', tenantId);

// Try to get the single configuration with full details
const url = `${apiUrl}/v1/inventory/configuration/${configId}?tenants=${tenantId}`;
console.log('\nURL:', url);

fetch(url, {
  headers: {
    'Authorization': `Basic ${credentials}`,
    'Accept': 'application/json',
  }
})
.then(async response => {
  console.log('\nStatus:', response.status, response.statusText);
  const text = await response.text();
  
  if (!response.ok) {
    console.error('Error:', text);
    return;
  }
  
  try {
    const data = JSON.parse(text);
    console.log('\n=== Configuration Response ===');
    console.log(JSON.stringify(data, null, 2));
    
    // Check if configuration text is included
    if (data.data && data.data.attributes) {
      const attrs = data.data.attributes;
      console.log('\n=== Configuration Details ===');
      console.log('Backup Time:', attrs.backupTime);
      console.log('Is Running Config:', attrs.isRunning);
      
      if (attrs.configText) {
        console.log('\n=== CONFIGURATION TEXT ===');
        console.log(attrs.configText);
      } else if (attrs.configBase64) {
        console.log('\n=== CONFIGURATION (Base64) ===');
        const decoded = Buffer.from(attrs.configBase64, 'base64').toString('utf-8');
        console.log(decoded);
      } else {
        console.log('\nNo configuration text found in attributes');
        console.log('Available attributes:', Object.keys(attrs));
      }
    }
  } catch (e) {
    console.log('Response:', text);
  }
})
.catch(error => {
  console.error('Fetch error:', error);
});
