/**
 * Safe migration script - checks if columns exist before renaming
 */
const { sequelize } = require('./src/config/database');

async function runMigration() {
  try {
    console.log('Connecting to database...');
    await sequelize.authenticate();
    console.log('Connected successfully!\n');

    // Check and rename hot_services.name_pt to name_es
    console.log('Checking hot_services table...');
    const [hotServiceCols] = await sequelize.query(
      "SHOW COLUMNS FROM `hot_services` LIKE 'name_pt'"
    );
    
    if (hotServiceCols.length > 0) {
      console.log('Renaming hot_services.name_pt to name_es...');
      await sequelize.query(
        "ALTER TABLE `hot_services` CHANGE COLUMN `name_pt` `name_es` VARCHAR(100) NOT NULL"
      );
      console.log('✓ hot_services.name_pt renamed to name_es');
    } else {
      console.log('✓ hot_services.name_es already exists, skipping...');
    }

    // Check and rename category.name_pt to name_es
    console.log('\nChecking category table...');
    const [categoryCols] = await sequelize.query(
      "SHOW COLUMNS FROM `category` LIKE 'name_pt'"
    );
    
    if (categoryCols.length > 0) {
      console.log('Renaming category.name_pt to name_es...');
      await sequelize.query(
        "ALTER TABLE `category` CHANGE COLUMN `name_pt` `name_es` VARCHAR(100) NOT NULL"
      );
      console.log('✓ category.name_pt renamed to name_es');
    } else {
      console.log('✓ category.name_es already exists, skipping...');
    }

    // Check and rename service.title_pt and description_pt
    console.log('\nChecking service table...');
    const [serviceTitleCols] = await sequelize.query(
      "SHOW COLUMNS FROM `service` LIKE 'title_pt'"
    );
    
    if (serviceTitleCols.length > 0) {
      console.log('Renaming service.title_pt to title_es...');
      await sequelize.query(
        "ALTER TABLE `service` CHANGE COLUMN `title_pt` `title_es` VARCHAR(200) NOT NULL"
      );
      console.log('✓ service.title_pt renamed to title_es');
    } else {
      console.log('✓ service.title_es already exists, skipping...');
    }

    const [serviceDescCols] = await sequelize.query(
      "SHOW COLUMNS FROM `service` LIKE 'description_pt'"
    );
    
    if (serviceDescCols.length > 0) {
      console.log('Renaming service.description_pt to description_es...');
      await sequelize.query(
        "ALTER TABLE `service` CHANGE COLUMN `description_pt` `description_es` TEXT"
      );
      console.log('✓ service.description_pt renamed to description_es');
    } else {
      console.log('✓ service.description_es already exists, skipping...');
    }

    // Check and rename notification fields
    console.log('\nChecking notification table...');
    const [notifTitleCols] = await sequelize.query(
      "SHOW COLUMNS FROM `notification` LIKE 'title_pt'"
    );
    
    if (notifTitleCols.length > 0) {
      console.log('Renaming notification.title_pt to title_es...');
      await sequelize.query(
        "ALTER TABLE `notification` CHANGE COLUMN `title_pt` `title_es` VARCHAR(200) NOT NULL"
      );
      console.log('✓ notification.title_pt renamed to title_es');
    } else {
      console.log('✓ notification.title_es already exists, skipping...');
    }

    const [notifContentCols] = await sequelize.query(
      "SHOW COLUMNS FROM `notification` LIKE 'content_pt'"
    );
    
    if (notifContentCols.length > 0) {
      console.log('Renaming notification.content_pt to content_es...');
      await sequelize.query(
        "ALTER TABLE `notification` CHANGE COLUMN `content_pt` `content_es` TEXT"
      );
      console.log('✓ notification.content_pt renamed to content_es');
    } else {
      console.log('✓ notification.content_es already exists, skipping...');
    }

    // Update user language preferences
    console.log('\nUpdating user language preferences...');
    const [result] = await sequelize.query(
      "UPDATE `user` SET language = 'es' WHERE language = 'pt'"
    );
    console.log(`✓ Updated ${result.affectedRows || 0} users from 'pt' to 'es'`);

    console.log('\n========================================');
    console.log('Migration completed successfully!');
    console.log('========================================');
    
    process.exit(0);
  } catch (error) {
    console.error('\nMigration failed:', error.message);
    process.exit(1);
  }
}

runMigration();
