'use strict';

/**
 * Migration to remove redundant deletion tracking columns.
 *
 * The `is_deleted` and `deleted_at_time` columns are redundant because:
 * - Sequelize's `paranoid: true` mode already uses `deletedAt` for soft-delete
 * - These columns were set but never queried for filtering
 *
 * IMPORTANT: This migration should only be run after verifying no external
 * systems depend on these columns. Consider backing up data first.
 */

module.exports = {
  async up(queryInterface) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      // Helper to safely remove column if it exists
      const removeColumnIfExists = async (tableName, columnName) => {
        const [results] = await queryInterface.sequelize.query(
          `SELECT column_name FROM information_schema.columns
           WHERE table_name = '${tableName}' AND column_name = '${columnName}'`,
          { transaction },
        );

        if (results.length > 0) {
          await queryInterface.removeColumn(tableName, columnName, {
            transaction,
          });
          console.log(`Removed column: ${tableName}.${columnName}`);
        } else {
          console.log(
            `Column does not exist (skipping): ${tableName}.${columnName}`,
          );
        }
      };

      // Remove is_deleted index from assets first (if exists)
      try {
        await queryInterface.removeIndex('assets', 'assets_is_deleted', {
          transaction,
        });
        console.log('Removed index: assets_is_deleted');
      } catch (error) {
        console.log('Index assets_is_deleted not found (may not exist)');
      }

      // Remove redundant columns from assets table
      await removeColumnIfExists('assets', 'is_deleted');
      await removeColumnIfExists('assets', 'deleted_at_time');

      // Remove redundant columns from projects table
      await removeColumnIfExists('projects', 'is_deleted');
      await removeColumnIfExists('projects', 'deleted_at_time');

      await transaction.commit();
      console.log('Redundant deletion columns removed successfully');
    } catch (error) {
      await transaction.rollback();
      throw error;
    }
  },

  async down(queryInterface, Sequelize) {
    const transaction = await queryInterface.sequelize.transaction();

    try {
      // Re-add columns to assets table
      await queryInterface.addColumn(
        'assets',
        'is_deleted',
        {
          type: Sequelize.BOOLEAN,
          allowNull: false,
          defaultValue: false,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'assets',
        'deleted_at_time',
        {
          type: Sequelize.DATE,
          allowNull: true,
        },
        { transaction },
      );

      // Re-add index
      await queryInterface.addIndex('assets', ['is_deleted'], {
        name: 'assets_is_deleted',
        transaction,
      });

      // Re-add columns to projects table
      await queryInterface.addColumn(
        'projects',
        'is_deleted',
        {
          type: Sequelize.BOOLEAN,
          allowNull: false,
          defaultValue: false,
        },
        { transaction },
      );

      await queryInterface.addColumn(
        'projects',
        'deleted_at_time',
        {
          type: Sequelize.DATE,
          allowNull: true,
        },
        { transaction },
      );

      await transaction.commit();
      console.log('Redundant deletion columns restored successfully');
    } catch (error) {
      await transaction.rollback();
      throw error;
    }
  },
};
