<commit_before># frozen_string_literal: true

class DomainTagsController < ApplicationController
  before_action :authenticate_user!, only: [:add, :remove, :edit, :update, :destroy, :add_post, :remove_post]
  before_action :verify_core, only: [:add, :remove, :edit, :update, :add_post, :remove_post]
  before_action :verify_admin, only: [:destroy]
  before_action :set_domain_tag, only: [:show, :edit, :update, :destroy]

  def index
    @tags = if params[:filter].present?
              DomainTag.where('name LIKE ?', "%#{params[:filter]}%")
            else
              DomainTag.all
            end.order(name: :asc).paginate(page: params[:page], per_page: 100)
    @counts = DomainTag.where(id: @tags.map(&:id)).joins(:spam_domains).group(Arel.sql('domain_tags.id')).count
  end

  def add
    @domain = SpamDomain.find params[:domain_id]
    @domain.domain_tags = params[:tag_name].map do |tag_name|
      DomainTag.find_or_create_by name: tag_name
    end
    @domain.save
    redirect_to spam_domain_path(@domain)
  end

  def add_review
    @tag = DomainTag.find_or_create_by name: params[:tag_name]
    @domain = SpamDomain.find params[:domain_id]
    @domain.domain_tags << @tag unless @domain.domain_tags.include? @tag
    render 'domain_tags/_tag', locals: { tag: @tag, domain: @domain }, layout: nil
  end

  def add_post
    @tag = DomainTag.find_or_create_by name: params[:tag_name]
    @post = Post.find params[:post_id]
    if @post.post_tags.include? @tag
      flash[:danger] = "This post already has the tag '#{@tag.name}'."
    else
      @post.post_tags << @tag
    end
    redirect_to post_path(@post)
  end

  def remove
    @domain = SpamDomain.find params[:domain_id]
    @domain.domain_tags.delete(DomainTag.find_by(name: params[:tag_name]))
    redirect_to spam_domain_path(@domain)
  end

  def remove_post
    @post = Post.find params[:post_id]
    @post.post_tags.delete(DomainTag.find_by(name: params[:tag_name]))
    redirect_back fallback_location: post_path(@post)
  end

  def show
    if params[:what] == 'posts'
      @posts = @tag.posts.includes_for_post_row.paginate(page: params[:page], per_page: 100)
      @sites = Site.where(id: @posts.map(&:site_id))
    else
      @domains = @tag.spam_domains
      puts @domains
      @domains = @domains.paginate(page: params[:page], per_page: 100)
      @counts = SpamDomain.where(id: @domains.map(&:id)).joins(:posts).group(Arel.sql('spam_domains.id')).count
    end
  end

  def edit; end

  def update
    if @tag.update tag_params
      flash[:success] = 'Updated tag.'
      redirect_to domain_tag_path(@tag)
    else
      flash[:danger] = 'Failed to update tag.'
      render :edit
    end
  end

  def destroy
    if @tag.destroy
      flash[:success] = 'Removed tag.'
      redirect_to domain_tags_path
    else
      flash[:danger] = 'Failed to remove tag.'
      render :show
    end
  end

  def mass_tagging
    @domains = if params[:filter].present?
                 mass_tag_filter params[:filter]<commit_msg>Removed debugging puts<commit_after># frozen_string_literal: true

class DomainTagsController < ApplicationController
  before_action :authenticate_user!, only: [:add, :remove, :edit, :update, :destroy, :add_post, :remove_post]
  before_action :verify_core, only: [:add, :remove, :edit, :update, :add_post, :remove_post]
  before_action :verify_admin, only: [:destroy]
  before_action :set_domain_tag, only: [:show, :edit, :update, :destroy]

  def index
    @tags = if params[:filter].present?
              DomainTag.where('name LIKE ?', "%#{params[:filter]}%")
            else
              DomainTag.all
            end.order(name: :asc).paginate(page: params[:page], per_page: 100)
    @counts = DomainTag.where(id: @tags.map(&:id)).joins(:spam_domains).group(Arel.sql('domain_tags.id')).count
  end

  def add
    @domain = SpamDomain.find params[:domain_id]
    @domain.domain_tags = params[:tag_name].map do |tag_name|
      DomainTag.find_or_create_by name: tag_name
    end
    @domain.save
    redirect_to spam_domain_path(@domain)
  end

  def add_review
    @tag = DomainTag.find_or_create_by name: params[:tag_name]
    @domain = SpamDomain.find params[:domain_id]
    @domain.domain_tags << @tag unless @domain.domain_tags.include? @tag
    render 'domain_tags/_tag', locals: { tag: @tag, domain: @domain }, layout: nil
  end

  def add_post
    @tag = DomainTag.find_or_create_by name: params[:tag_name]
    @post = Post.find params[:post_id]
    if @post.post_tags.include? @tag
      flash[:danger] = "This post already has the tag '#{@tag.name}'."
    else
      @post.post_tags << @tag
    end
    redirect_to post_path(@post)
  end

  def remove
    @domain = SpamDomain.find params[:domain_id]
    @domain.domain_tags.delete(DomainTag.find_by(name: params[:tag_name]))
    redirect_to spam_domain_path(@domain)
  end

  def remove_post
    @post = Post.find params[:post_id]
    @post.post_tags.delete(DomainTag.find_by(name: params[:tag_name]))
    redirect_back fallback_location: post_path(@post)
  end

  def show
    if params[:what] == 'posts'
      @posts = @tag.posts.includes_for_post_row.paginate(page: params[:page], per_page: 100)
      @sites = Site.where(id: @posts.map(&:site_id))
    else
      @domains = @tag.spam_domains.paginate(page: params[:page], per_page: 100)
      @counts = SpamDomain.where(id: @domains.map(&:id)).joins(:posts).group(Arel.sql('spam_domains.id')).count
    end
  end

  def edit; end

  def update
    if @tag.update tag_params
      flash[:success] = 'Updated tag.'
      redirect_to domain_tag_path(@tag)
    else
      flash[:danger] = 'Failed to update tag.'
      render :edit
    end
  end

  def destroy
    if @tag.destroy
      flash[:success] = 'Removed tag.'
      redirect_to domain_tags_path
    else
      flash[:danger] = 'Failed to remove tag.'
      render :show
    end
  end

  def mass_tagging
    @domains = if params[:filter].present?
                 mass_tag_filter params[:filter]
{'fraction_non_alphanumeric': 0.1297441899347935, 'fraction_numerical': 0.0030095301788998496, 'mean_word_length': 3.51131221719457, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4727134', 'n_tokens_mistral': 2073, 'n_tokens_neox': 1991, 'n_words': 439}