# typed: strict
class Domain::UserJobEventsController < DomainController
  extend T::Sig

  before_action :set_user!

  sig { void }
  def tracked_objects_kinds
    @kinds =
      T.let(
        Domain::UserJobEvent::AddTrackedObject.kinds.keys.map(&:to_s),
        T.nilable(T::Array[String]),
      )

    @kind_counts =
      T.let(
        @user&.add_tracked_objects&.reorder(nil)&.group(:kind)&.count,
        T.nilable(T::Hash[String, Integer]),
      )

    @kinds_most_at =
      T.let(
        @user
          &.add_tracked_objects
          &.reorder(nil)
          &.group(:kind)
          &.maximum(:requested_at),
        T.nilable(T::Hash[String, Time]),
      )
  end

  sig { void }
  def tracked_objects
    set_and_validate_kind!
    @tracked_objects =
      T.let(
        T
          .must(@user)
          .add_tracked_objects
          .includes(:log_entry)
          .where(kind: @kind)
          .sort_by { |a| -a.requested_at.to_i },
        T.untyped,
      )
  end

  sig { void }
  def backfill_scan_job
    set_and_validate_kind!
    @user = T.must(@user)

    unless @user.is_a?(Domain::User::FaUser)
      flash[:error] = "This user is not a FurAffinity user"
      redirect_to tracked_objects_domain_user_job_events_path(
                    @user,
                    kind: @kind,
                  )
      return
    end

    case @kind
    when "favs"
      now = Time.now
      stats = Domain::Fa::BackfillTrackedObjectUserFavs.new(user: @user).run
      flash[
        :success
      ] = "Backfilled #{@user.url_name} favs, #{stats.total_created} favs scans created, #{stats.total_favs} favs, loaded #{stats.total_hles} logs, took #{helpers.distance_of_time_in_words_to_now(now, include_seconds: true)}"
    end

    redirect_to tracked_objects_domain_user_job_events_path(@user, kind: @kind)
  end

  sig { void }
  def enqueue_scan_job
    set_and_validate_kind!
    @user = T.must(@user)

    unless @user.is_a?(Domain::User::FaUser)
      flash[:error] = "This user is not a FurAffinity user"
      redirect_to tracked_objects_domain_user_job_events_path(
                    @user,
                    kind: @kind,
                  )
      return
    end

    case @kind
    when "favs"
      flash[:success] = "Enqueued scan job for #{@user.url_name} favs"
      Domain::Fa::Job::FavsJob.set(queue: "manual").perform_later(
        user: @user,
        force_scan: true,
      )
    else
      flash[:error] = "Unimplemented kind: #{@kind}"
    end

    redirect_to tracked_objects_domain_user_job_events_path(@user, kind: @kind)
  end

  private

  sig { override.returns(DomainController::DomainParamConfig) }
  def self.param_config
    DomainController::DomainParamConfig.new(
      user_id_param: :domain_user_id,
      post_id_param: :domain_post_id,
      post_group_id_param: :domain_post_group_id,
    )
  end

  sig { void }
  def set_and_validate_kind!
    @kind = T.let(params[:kind], T.nilable(String))
    raise ActionController::RoutingError, "Not Found" if @kind.blank?
    unless Domain::UserJobEvent::AddTrackedObject.kinds.include?(@kind)
      raise ActionController::RoutingError, "Not Found"
    end
  end
end
