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

async function fixColumns() {
  const nocodbToken = 'owuYNodz0RcnDtUqnj5DK4Qeyp3ASQkDYkrdfGtw';
  
  console.log('Fetching columns...');
  
  // Get all columns first
  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.columns.find(c => c.title === 'type');
  const severityCol = cols.data.columns.find(c => c.title === 'severity');
  const statusCol = cols.data.columns.find(c => c.title === 'status');
  
  console.log('Found columns:');
  console.log('type ID:', typeCol?.id, 'dtxp:', typeCol?.dtxp);
  console.log('severity ID:', severityCol?.id, 'dtxp:', severityCol?.dtxp);
  console.log('status ID:', statusCol?.id, 'dtxp:', statusCol?.dtxp);
  
  // Fix type column
  if (typeCol) {
    console.log('\nFixing type column...');
    const resp = 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' }}
    );
    const updated = resp.data.columns.find(c => c.title === 'type');
    console.log('type dtxp now:', updated?.dtxp);
  }
  
  // Fix severity column
  if (severityCol) {
    console.log('\nFixing severity column...');
    const resp = 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' }}
    );
    const updated = resp.data.columns.find(c => c.title === 'severity');
    console.log('severity dtxp now:', updated?.dtxp);
  }
  
  // Fix status column
  if (statusCol) {
    console.log('\nFixing status column...');
    const resp = 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' }}
    );
    const updated = resp.data.columns.find(c => c.title === 'status');
    console.log('status dtxp now:', updated?.dtxp);
  }
  
  console.log('\nDone!');
}

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