# frozen_string_literal: true

class EventNormalizationJob < ApplicationJob
  queue_as :default

  # Normalize all existing events
  def perform_all_events(batch_size: 1000)
    total_events = Event.where(request_host_id: nil).count
    Rails.logger.info "Starting normalization of #{total_events} events"

    offset = 0
    processed = 0

    loop do
      events = Event.where(request_host_id: nil)
                   .limit(batch_size)
                   .offset(offset)

      break if events.empty?

      events.each do |event|
        begin
          EventNormalizer.normalize_event!(event)
          event.save!
          processed += 1
        rescue => e
          Rails.logger.error "Failed to normalize event #{event.id}: #{e.message}"
        end
      end

      Rails.logger.info "Processed #{processed}/#{total_events} events"
      offset += batch_size
    end

    Rails.logger.info "Completed normalization of #{processed} events"
  end

  # Normalize a specific event
  def perform(event_id)
    event = Event.find(event_id)

    EventNormalizer.normalize_event!(event)
    event.save!

    Rails.logger.info "Successfully normalized event #{event_id}"
  rescue ActiveRecord::RecordNotFound
    Rails.logger.error "Event #{event_id} not found for normalization"
  rescue => e
    Rails.logger.error "Failed to normalize event #{event_id}: #{e.message}"
    raise
  end

  # Normalize events for a specific project
  def perform_for_project(project_id, batch_size: 1000)
    project = Project.find(project_id)
    total_events = project.events.where(request_host_id: nil).count
    Rails.logger.info "Starting normalization of #{total_events} events for project #{project.name}"

    offset = 0
    processed = 0

    loop do
      events = project.events
                      .where(request_host_id: nil)
                      .limit(batch_size)
                      .offset(offset)

      break if events.empty?

      events.each do |event|
        begin
          EventNormalizer.normalize_event!(event)
          event.save!
          processed += 1
        rescue => e
          Rails.logger.error "Failed to normalize event #{event.id}: #{e.message}"
        end
      end

      Rails.logger.info "Processed #{processed}/#{total_events} events for project #{project.name}"
      offset += batch_size
    end

    Rails.logger.info "Completed normalization of #{processed} events for project #{project.name}"
  end
end