namespace :new_radio_profile_network do
  desc "Setting up redis default info details for existing device"
  task new_radio_profile_to_network: :environment do
    organisation = Organisation.all
      organisation.each do |org|
        radio_profile = RadioProfile.where(:id =>org.radio_profiles.pluck(:id))
        radio_profile.each do |radio|
          ris_config  = radio.router_inventories.group_by(&:location_network)
          lns_config = radio.location_networks
          total_mappings = (ris_config.keys+ lns_config).uniq.compact
          if total_mappings.present?
            if total_mappings.count > 1
              total_mappings.each do |network|
                radio_dup = radio.dup
                radio_settings = radio.radio_settings.map{|settings| settings.dup}
                ## radio profiles New##
                resources = []
                resources << ["network:#{network.id}"] if lns_config.include?(network).present?
                resources << ris_config[network].compact.map {|ri| "AP:#{ri.id}"} if ris_config[network].present?
                if resources.flatten.present?
                  radio_dup.associated_resources = resources.flatten.uniq
                  radio_dup.location_network_id = network.id
                  radio_dup.radio_settings = radio_settings
                  radio_dup.save
                end
              end
            else
              radio.location_network_id =  total_mappings.first.id
              radio.save
            end
          end
        end
      end
    
  end
end

