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

async function fix() {
  // 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'];
  
  // Get table metadata
  console.log('Getting table metadata...');
  const table = await axios.get('http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys', {
    headers: { 'xc-token': nocodbToken }
  });
  
  console.log('Columns:', table.data.columns?.map(c => c.column_name) || 'No columns in response');
  
  // Find and update columns
  const columns = table.data.columns || [];
  
  for (const col of columns) {
    if (col.column_name === 'type') {
      console.log('Updating type column...');
      await axios.patch(
        `http://192.168.25.5:8080/api/v2/meta/columns/${col.id}`,
        { dtxp: 'correction,preference,episode,decision,validation' },
        { headers: { 'xc-token': nocodbToken, 'Content-Type': 'application/json' }}
      );
      console.log('✅ Type updated');
    }
    
    if (col.column_name === 'severity') {
      console.log('Updating severity column...');
      await axios.patch(
        `http://192.168.25.5:8080/api/v2/meta/columns/${col.id}`,
        { dtxp: 'error,warning,auto-correct' },
        { headers: { 'xc-token': nocodbToken, 'Content-Type': 'application/json' }}
      );
      console.log('✅ Severity updated');
    }
    
    if (col.column_name === 'status') {
      console.log('Updating status column...');
      await axios.patch(
        `http://192.168.25.5:8080/api/v2/meta/columns/${col.id}`,
        { dtxp: 'active,reversed,deprecated' },
        { headers: { 'xc-token': nocodbToken, 'Content-Type': 'application/json' }}
      );
      console.log('✅ Status updated');
    }
  }
}

fix().catch(err => {
  console.error('Error:', err.response?.data || err.message);
});
