# frozen_string_literal: true

class ArticlesController < ApplicationController
  # skip_before_action :authenticate_user!, only: %i[index show]


  def new
    @article = Article.new(impact: :no, products: Product.where(name: "TiDB").to_a)
  end

  def index
    query = params[:query] || "*"

    wheres = if current_user
      if current_user.admin_group?
        {}
      else
        { _or: [{ verified_status: :verified }, { user_id: current_user.id }] }
      end
    else
      { verified_status: :verified }
    end

    if params[:product].present?
      if product = Product.where(name: params[:product]).first
        wheres[:product_ids] = [product.id]
      end
    end

    if params[:component].present?
      if component = Component.where(name: params[:component]).first
        wheres[:component_ids] = [component.id]
      end
    end

    if params[:category].present?
      if category = Category.where(name: params[:category]).first
        wheres[:category_ids] = [category.id]
      end
    end

    if params[:tag].present?
      if tag = Tag.where(name: params[:tag]).first
        wheres[:tag_ids] = [tag.id]
      end
    end

    sorts = if params[:sort_type].blank?
      { created_at: :desc }
    elsif params[:sort_type] == "most_relevant"
      { _score: :desc }
    elsif params[:sort_type] == "newest_first"
      { created_at: :desc }
    elsif params[:sort_type] == "oldest_first"
      { created_at: :asc }
    else
      { created_at: :desc }
    end

    results = Article.pagy_search(query, where: wheres, order: sorts, debug: true)

    @pagy, @articles = pagy_searchkick(
      results,
      items: 10
    )
  end

  def approve
    @article = authorize Article.find(params[:id])
    @article.update(verified_status: :verified)
    inbox = @article.user.inboxes.create(article: @article, from_user: current_user, to_user: @article.user, box_type: "Approve")
    InboxMailer.with(user: @article.user, inbox: inbox).notify.deliver_later
    flash[:success] = "Approved"
    audit! :approve, @article
    redirect_back(fallback_location: root_path)
  end

  def disapprove
    @article = authorize Article.find(params[:id])
    @article.update(verified_status: :unverified)
    inbox = @article.user.inboxes.create(article: @article, from_user: current_user, to_user: @article.user, box_type: "Disapprove")
    InboxMailer.with(user: @article.user, inbox: inbox).notify.deliver_later
    flash[:success] = "Disapproved"
    audit! :disapprove, @article
    redirect_back(fallback_location: root_path)
  end

  def show
    @article = authorize Article.find(params[:id])
    @comments = @article.comments.order(id: :asc)
    @user = @article.user
  end

  def edit
    @article = authorize Article.find(params[:id])
  end

  def update
    @article = authorize Article.find(params[:id])
    if @article.update(article_params)
      audit! :update, @article
      flash[:success] = t("article_updated")
      redirect_to article_path(@article)
    else
      flash[:error] = @article.errors.full_messages.to_sentence
      render "edit"
    end
  end


  def create
    @article = current_user.articles.new(article_params.merge(verified_status: :unverified))
    if @article.save
      flash[:success] = t("article_added")
      audit! :create, @article
      User.reviewers.each do |user|
        inbox = user.inboxes.create(article: @article, from_user: current_user, to_user: user, box_type: "Review")
        InboxMailer.with(user: user, inbox: inbox).notify.deliver_later
      end
      redirect_to article_path(@article)
    else
      flash[:error] = @article.errors.full_messages.to_sentence
      render "new"
    end
  end

  def toggle_useful
    @article = Article.find(params[:id])
    if @article.useful_user_ids.include?(current_user.id)
      @useful = false
      @article.usefuls.where(user: current_user).first&.destroy
    else
      @useful = true
      useful = @article.usefuls.create(user: current_user)
      inbox = @article.user.inboxes.create(article: @article, from_user: current_user, to_user: @article.user, box_type: "Useful")
      InboxMailer.with(user: @article.user, inbox: inbox).notify.deliver_later
    end
    render partial: "articles/useful", content_type: "text/html"
  end

  private

    def article_params
      params.require(:article).permit(
        :title,
        :tag_names,
        :category_names,
        :product_names,
        :component_names,
        :versions_affected,
        :issue,
        :root_cause,
        :diagnostic_steps,
        :impact,
        :backtrack_link,
        :solution
      )
    end
end
