namespace :katello do

desc "Enable or disable the use of structured content for APT clients."
task :enable_structured_content_for_deb, ['deb_enable_structured_apt'] => ['environment', 'dynflow:client', "check_ping"] do |_t, args|
  unless ['true', 'false'].include?(args[:deb_enable_structured_apt])
    puts "You must specify if structured content should be enabled or disabled, your options are:"
    puts "  foreman-rake katello:enable_structured_content_for_deb[true]"
    puts "  foreman-rake katello:enable_structured_content_for_deb[false]"
    puts "Note that use of structured content is currently set to '#{Setting['deb_enable_structured_apt']}'!"
    puts "Note that after enabling structured content, you may need to resync your proxies!"
    exit 1
  end

  # TODO: Should we add a guard in case there are already running foreman tasks? We don't want race conditions!

  User.current = User.anonymous_api_admin
  deb_enable_structured_apt = ActiveModel::Type::Boolean.new.cast(args[:deb_enable_structured_apt])

  # Update deb_enable_structured_apt to the value requested by the user:
  Setting['deb_enable_structured_apt'] = deb_enable_structured_apt

  roots = Katello::RootRepository.deb_type
  roots.each do |root|
    # We must handle the library instance first!
    library_instance = root.library_instance
    if deb_enable_structured_apt
      ForemanTasks.sync_task(::Actions::Katello::Product::ContentDestroy, library_instance)
      root.content_id = nil
      root.save!
    else
      root.content_id = library_instance.content_id
      library_instance.content_id = nil
      root.save!
      library_instance.save!
      ForemanTasks.sync_task(::Actions::Katello::Repository::Update, root, {})
    end

    repos = root.repositories
    if deb_enable_structured_apt
      repos.each do |repo|
        content_create = ForemanTasks.sync_task(::Actions::Katello::Product::ContentCreate, repo)
        content_id = content_create.input[:content_id]
        content_view_environment = repo.content_view_environment
        if content_view_environment
          ForemanTasks.sync_task(::Actions::Candlepin::Environment::AddContentToEnvironment, :view_env_cp_id => content_view_environment.cp_id, :content_id => content_id)
        end
      end
    else
      # We need to enable in order to delete structured content using ContentDestroy.
      Setting['deb_enable_structured_apt'] = true
      repos.each do |repo|
        next if repo.library_instance?

        content_view_environment = repo.content_view_environment
        if content_view_environment
          ForemanTasks.sync_task(::Actions::Candlepin::Environment::AddContentToEnvironment, :view_env_cp_id => content_view_environment.cp_id, :content_id => root.content_id)
        end
        ForemanTasks.sync_task(::Actions::Katello::Product::ContentDestroy, repo)
        repo.content_id = nil
        repo.save!
      end
      # And disable once more
      Setting['deb_enable_structured_apt'] = false
    end
  end

  # TODO: Should we trigger smart proxy syncs here or leave this up to users?
end

end