import { sequelize } from '../src/models/index.js';

async function migrate() {
  try {
    await sequelize.query(
      "ALTER TABLE bookings ADD COLUMN booking_type ENUM('workshop','home') NOT NULL DEFAULT 'workshop' AFTER service_id;"
    );
  } catch (err) {
    if (!err.message.includes('Duplicate column name')) {
      console.error('Failed adding booking_type:', err.message);
      process.exit(1);
    }
  }

  try {
    await sequelize.query(
      'ALTER TABLE bookings ADD COLUMN home_address VARCHAR(500) NULL AFTER booking_type;'
    );
  } catch (err) {
    if (!err.message.includes('Duplicate column name')) {
      console.error('Failed adding home_address:', err.message);
      process.exit(1);
    }
  }

  try {
    await sequelize.query(
      'ALTER TABLE bookings ADD COLUMN contact_name VARCHAR(120) NULL AFTER home_address;'
    );
  } catch (err) {
    if (!err.message.includes('Duplicate column name')) {
      console.error('Failed adding contact_name:', err.message);
      process.exit(1);
    }
  }

  try {
    await sequelize.query(
      'ALTER TABLE bookings ADD COLUMN contact_phone VARCHAR(20) NULL AFTER contact_name;'
    );
  } catch (err) {
    if (!err.message.includes('Duplicate column name')) {
      console.error('Failed adding contact_phone:', err.message);
      process.exit(1);
    }
  }

  try {
    await sequelize.query(
      'ALTER TABLE bookings ADD COLUMN customer_note VARCHAR(500) NULL AFTER contact_phone;'
    );
  } catch (err) {
    if (!err.message.includes('Duplicate column name')) {
      console.error('Failed adding customer_note:', err.message);
      process.exit(1);
    }
  }

  console.log('Home booking fields migration completed.');
  process.exit(0);
}

migrate();
