import pg from 'pg';
const { Pool } = pg;

const liaraPool = new Pool({
  connectionString: 'postgresql://root:hePGFWh8f5TAEWCBK0V8XtQh@el-capitan.liara.cloud:32727/postgres',
  ssl: false,
});

const vpsPool = new Pool({
  connectionString: 'postgresql://user:d1ph12poyhd1o2pDASd2jp2dnbcba@94.101.186.198:5433/steelforesight',
  ssl: false,
});

async function migrateDb() {
  console.log('Connecting to databases...');
  
  // 1. Get all tables in Liara
  const tablesRes = await liaraPool.query(`
    SELECT table_name 
    FROM information_schema.tables 
    WHERE table_schema = 'public' AND table_type = 'BASE TABLE'
    ORDER BY table_name;
  `);
  
  const tables = tablesRes.rows.map(r => r.table_name);
  console.log('Tables to migrate:', tables);

  // 2. For each table, get column definitions & create table on VPS if not exists
  for (const table of tables) {
    console.log(`\n--- Migrating table: ${table} ---`);
    
    // Get column names and types
    const colsRes = await liaraPool.query(`
      SELECT column_name, data_type, udt_name, is_nullable, column_default
      FROM information_schema.columns
      WHERE table_schema = 'public' AND table_name = $1
      ORDER BY ordinal_position;
    `, [table]);

    // Fetch all rows
    const dataRes = await liaraPool.query(`SELECT * FROM "${table}"`);
    console.log(`Found ${dataRes.rows.length} rows in ${table}`);

    // Drop table on VPS if exists and create fresh
    // We can extract table DDL or create from columns
    if (dataRes.rows.length > 0) {
      const colNames = Object.keys(dataRes.rows[0]);
      
      // Let's create the table with db.js initialization or dynamic columns
      // But simpler: let db.js initialize tables on VPS first, or execute DDL
    }
  }
}

migrateDb().catch(console.error);
