const { Pool } = require("pg");
const pool = new Pool({
  user: "postgres",
  password: "Sethuds@12",
  host: "localhost",
  port: 5432,
  database: "textile"
});

async function migrate() {
  try {
    // 1. Add address column
    console.log("Adding address column...");
    await pool.query("ALTER TABLE customer_master ADD COLUMN IF NOT EXISTS address text;");
    
    // 2. Drop other columns
    console.log("Dropping old address columns...");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS flat_no;");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS build_no;");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS street;");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS city;");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS state;");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS country;");
    await pool.query("ALTER TABLE customer_master DROP COLUMN IF EXISTS pincode;");

    console.log("Migration complete!");
    
    // 3. Verify columns
    const res = await pool.query(`
      SELECT column_name, data_type 
      FROM information_schema.columns 
      WHERE table_name = 'customer_master'
    `);
    console.log("NEW COLUMNS IN customer_master:");
    console.log(JSON.stringify(res.rows, null, 2));
  } catch (err) {
    console.error("Migration Error:", err);
  } finally {
    await pool.end();
  }
}

migrate();
