class MoveExistingRecordToConfigMapping < ActiveRecord::Migration[6.0]
  def change
    acl_ids = AclGroup.all.pluck(:id)
    acl_tags = AclGroup.all.pluck(:tagging_lists)
    loop_check acl_ids,acl_tags,"AclGroup"

    snmp_ids = SnmpConfig.all.pluck(:id)
    snmp_tags = SnmpConfig.all.pluck(:tagging_lists)
    loop_check snmp_ids,snmp_tags,"SnmpConfig"

    pf_ids = PortforwardingGroup.all.pluck(:id)
    pf_tags = PortforwardingGroup.all.pluck(:tagging_lists)
    loop_check pf_ids,pf_tags,"PortforwardingGroup"
  end

  def loop_check ids,tags,con_type
      ids.zip(tags).each do |lists|
      (lists[1] || ' ').split('&&').each do |list|
        ar = (list || ' ').split('--')
        if ar[1] == 'inventory'
          ConfigMapping.create!(configurable_id: lists[0] ,configurable_type: con_type,resourceable_id: ar[0] ,resourceable_type: "RouterInventory")
        elsif ar[1] == 'tag_list'
          id = ActsAsTaggableOn::Tag.where(:name => ar[0]).first.try(:id)
          ConfigMapping.create!(configurable_id: lists[0] ,configurable_type: con_type,resourceable_id: id ,resourceable_type: "ActsAsTaggableOn::Tag") unless id.blank?
        elsif ar[1] == 'network_name'
          ConfigMapping.create!(configurable_id: lists[0] ,configurable_type: con_type,resourceable_id: ar[0] ,resourceable_type: "LocationNetwork")
        end
      end
    end
  end
end
