#!/usr/bin/env node
const axios = require('axios');

async function debug() {
  // Get vault token
  const vaultResp = await axios.post('https://beavault.beawit.net:8200/v1/auth/approle/login', {
    role_id: '75d2dcfb-9c65-7f60-59b4-eee8c7f8dc0e',
    secret_id: '6202b465-2f25-547c-ec07-f47cfc4dda3e'
  }, { httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false }) });
  
  const vaultToken = vaultResp.data.auth.client_token;
  
  // Get nocodb token
  const infraResp = await axios.get('https://beavault.beawit.net:8200/v1/kv/data/api/infrastructure', {
    headers: { 'X-Vault-Token': vaultToken },
    httpsAgent: new (require('https').Agent)({ rejectUnauthorized: false })
  });
  
  const nocodbToken = infraResp.data.data.data['nocodb-token'];
  
  console.log('Token:', nocodbToken.substring(0, 20) + '...');
  
  // Check existing records
  console.log('\n=== Existing records ===');
  const records = await axios.get('http://192.168.25.5:8080/api/v2/tables/mx149yctebfwvys/records', {
    headers: { 'xc-token': nocodbToken }
  });
  console.log('Records:', JSON.stringify(records.data.list, null, 2));
  
  // Try to create with minimal payload
  console.log('\n=== Testing minimal insert ===');
  try {
    const result = await axios.post(
      'http://192.168.25.5:8080/api/v2/tables/mx149yctebfwvys/records',
      {
        type: 'correction',
        id: 'test_' + Date.now(),
        pattern: 'test',
        severity: 'auto-correct',
        message: 'test message',
        blocking: true
      },
      {
        headers: { 
          'xc-token': nocodbToken,
          'Content-Type': 'application/json'
        }
      }
    );
    console.log('Success:', result.data);
  } catch (err) {
    console.log('Error:', err.response?.data || err.message);
    console.log('Status:', err.response?.status);
  }
}

debug().catch(console.error);
