class AlterActivities < ActiveRecord::Migration
  def up
    UserActivity.all.each do |activity|
      if activity.act_type == 'JournalsForMessage'
        if activity.act
          unless activity.act.m_parent_id.nil?
            parent_act = UserActivity.where("act_id = #{activity.act.m_parent_id} and act_type='JournalsForMessage' and container_type='Course'").first
            if parent_act
              parent_act.created_at = activity.act.parent.children.maximum("created_on")
              parent_act.save
              activity.destroy
            end
          end
        else
          activity.destroy
        end
      end
    end

      CourseActivity.all.each do |activity|
        if activity.course_act_type == 'JournalsForMessage'
          if activity.course_act
            unless activity.course_act.m_parent_id.nil?
              parent_act = CourseActivity.where("course_act_id = #{activity.course_act.m_parent_id} and course_act_type='JournalsForMessage'").first
              if parent_act
                parent_act.created_at = activity.course_act.parent.children.maximum("created_on")
                parent_act.save
                activity.destroy
              end
            end
          else
            activity.destroy
          end
        elsif activity.course_act_type == 'Message'
          if activity.course_act
            unless activity.course_act.parent_id.nil?
              parent_act = CourseActivity.where("course_act_id = #{activity.course_act.parent_id} and course_act_type='Message'").first
              if parent_act
                parent_act.created_at = activity.course_act.parent.children.maximum("created_on")
                parent_act.save
                activity.destroy
              end
            end
          else
            activity.destroy
          end
        end
      end
  end

  def down
  end
end
