# frozen_string_literal: true

namespace :events do
  desc "Backfill network intelligence data for events"
  task backfill_network_intelligence: :environment do
    batch_size = ENV['BATCH_SIZE']&.to_i || 10_000
    Event.backfill_network_intelligence!(batch_size: batch_size)
  end

  desc "Backfill events with missing network intelligence (newly imported network data)"
  task backfill_missing: :environment do
    count = Event.where(country: nil).count

    if count.zero?
      puts "✓ No events missing network intelligence"
    else
      puts "Found #{count} events without network intelligence"
      puts "Backfilling..."

      processed = 0
      Event.where(country: nil).find_in_batches(batch_size: 1000) do |batch|
        batch.each(&:save)
        processed += batch.size
        puts "  Processed #{processed}/#{count}"
      end

      puts "✓ Complete"
    end
  end

  desc "Show backfill progress"
  task backfill_progress: :environment do
    total = Event.count
    with_country = Event.where.not(country: nil).count
    without_country = Event.where(country: nil).count
    percent = (with_country.to_f / total * 100).round(1)

    puts "=" * 60
    puts "Network Intelligence Backfill Progress"
    puts "=" * 60
    puts "Total events:           #{total}"
    puts "With network data:      #{with_country} (#{percent}%)"
    puts "Missing network data:   #{without_country}"
    puts "=" * 60

    if without_country > 0
      puts
      puts "To continue backfill:"
      puts "  rails events:backfill_network_intelligence"
      puts
      puts "Or with custom batch size:"
      puts "  BATCH_SIZE=5000 rails events:backfill_network_intelligence"
    else
      puts "✓ Backfill complete!"
    end
  end
end
