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

    try {
      const tables = await queryInterface.showAllTables({ transaction });
      const tableNames = tables.map((table) => (typeof table === 'object' ? table.tableName : table));

      if (!tableNames.includes('vorta_universe_states')) {
        await queryInterface.createTable(
          'vorta_universe_states',
          {
            id: {
              type: Sequelize.DataTypes.UUID,
              defaultValue: Sequelize.DataTypes.UUIDV4,
              primaryKey: true,
            },
            userId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'users',
                key: 'id',
              },
            },
            organizationsId: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'organizations',
                key: 'id',
              },
            },
            profile: {
              type: Sequelize.DataTypes.JSONB,
              allowNull: false,
              defaultValue: {},
            },
            modules: {
              type: Sequelize.DataTypes.JSONB,
              allowNull: false,
              defaultValue: [],
            },
            products: {
              type: Sequelize.DataTypes.JSONB,
              allowNull: false,
              defaultValue: [],
            },
            ledger: {
              type: Sequelize.DataTypes.JSONB,
              allowNull: false,
              defaultValue: [],
            },
            settings: {
              type: Sequelize.DataTypes.JSONB,
              allowNull: false,
              defaultValue: {},
            },
            importHash: {
              type: Sequelize.DataTypes.STRING(255),
              allowNull: true,
              unique: true,
            },
            createdById: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'users',
                key: 'id',
              },
            },
            updatedById: {
              type: Sequelize.DataTypes.UUID,
              references: {
                model: 'users',
                key: 'id',
              },
            },
            createdAt: {
              type: Sequelize.DataTypes.DATE,
            },
            updatedAt: {
              type: Sequelize.DataTypes.DATE,
            },
            deletedAt: {
              type: Sequelize.DataTypes.DATE,
            },
          },
          { transaction },
        );
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      console.error('Failed to create vorta_universe_states table:', error);
      throw error;
    }
  },

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

    try {
      const tables = await queryInterface.showAllTables({ transaction });
      const tableNames = tables.map((table) => (typeof table === 'object' ? table.tableName : table));

      if (tableNames.includes('vorta_universe_states')) {
        await queryInterface.dropTable('vorta_universe_states', { transaction });
      }

      await transaction.commit();
    } catch (error) {
      await transaction.rollback();
      console.error('Failed to drop vorta_universe_states table:', error);
      throw error;
    }
  },
};
