#!/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 columns to find type column ID
  console.log('Getting columns...');
  const cols = await axios.get('http://192.168.25.5:8080/api/v2/meta/tables/mx149yctebfwvys/columns', {
    headers: { 'xc-token': nocodbToken }
  });
  
  const typeCol = cols.data.list.find(c => c.title === 'type');
  console.log('Type column:', typeCol?.id, typeCol?.title);
  
  if (typeCol) {
    // Update column with options
    console.log('Updating type column with options...');
    await axios.patch(
      `http://192.168.25.5:8080/api/v2/meta/columns/${typeCol.id}`,
      {
        dtxp: 'correction,preference,episode,decision,validation'
      },
      {
        headers: { 
          'xc-token': nocodbToken,
          'Content-Type': 'application/json'
        }
      }
    );
    console.log('✅ Updated type column');
  }
  
  // Also fix severity column
  const severityCol = cols.data.list.find(c => c.title === 'severity');
  if (severityCol) {
    console.log('Updating severity column with options...');
    await axios.patch(
      `http://192.168.25.5:8080/api/v2/meta/columns/${severityCol.id}`,
      {
        dtxp: 'error,warning,auto-correct'
      },
      {
        headers: { 
          'xc-token': nocodbToken,
          'Content-Type': 'application/json'
        }
      }
    );
    console.log('✅ Updated severity column');
  }
  
  // Fix status column
  const statusCol = cols.data.list.find(c => c.title === 'status');
  if (statusCol) {
    console.log('Updating status column with options...');
    await axios.patch(
      `http://192.168.25.5:8080/api/v2/meta/columns/${statusCol.id}`,
      {
        dtxp: 'active,reversed,deprecated'
      },
      {
        headers: { 
          'xc-token': nocodbToken,
          'Content-Type': 'application/json'
        }
      }
    );
    console.log('✅ Updated status column');
  }
  
  console.log('\n=== Done ===');
}

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