# frozen_string_literal: true

class CommentsController < ApplicationController
  before_action :set_comment, only: %i[show update destroy report]

  def authorize!
    authorize(@comment || @comments)
  end
  private :authorize!

  # GET /comments.json
  def index
    file = CodeOcean::File.find(params[:file_id])
    # there might be no submission yet, so dont use find
    submission = Submission.find_by(id: file.context_id)
    if submission
      @comments = Comment.where(file_id: params[:file_id])
    else
      @comments = []
    end
    authorize!
  end

  # GET /comments/1.json
  def show
    authorize!
  end

  # POST /comments.json
  def create
    @comment = Comment.new(comment_params_without_request_id)

    if @comment.save
      if comment_params[:request_id]
        request_for_comment = RequestForComment.find(comment_params[:request_id])
        send_mail_to_author @comment, request_for_comment
        send_mail_to_subscribers @comment, request_for_comment
      end

      render :show, status: :created, location: @comment
    else
      render json: @comment.errors, status: :unprocessable_content
    end
    authorize!
  end

  # PATCH/PUT /comments/1.json
  def update
    if @comment.update(comment_params_for_update)
      render :show, status: :ok, location: @comment
    else
      render json: @comment.errors, status: :unprocessable_content
    end
    authorize!
  end

  # DELETE /comments/1.json
  def destroy
    authorize!
    @comment.destroy
    head :no_content
  end

  # POST /comments/1/report.json
  def report
    authorize!

    UserContentReportMailer.with(reported_content: @comment).report_content.deliver_later

    head :no_content
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_comment
    @comment = Comment.find(params[:id])
  end

  def comment_params_for_update
    params.expect(comment: [:text])
  end

  def comment_params_without_request_id
    comment_params.except :request_id
  end

  def comment_params
    params.expect(comment: %i[file_id row column text request_id]).merge(user: current_user)
  end

  def send_mail_to_author(comment, request_for_comment)
    if current_user != request_for_comment.user
      UserMailer.with(comment:, request_for_comment:, commenting_user: current_user).got_new_comment.deliver_later
    end
  end

  def send_mail_to_subscribers(comment, request_for_comment)
    request_for_comment.commenters.each do |commenter|
      already_sent_mail = false
      subscriptions = Subscription.where(
        request_for_comment_id: request_for_comment.id,
        user: commenter, deleted: false
      )
      subscriptions.each do |subscription|
        if (((subscription.subscription_type == 'author') && (current_user == request_for_comment.user)) || (subscription.subscription_type == 'all')) && !((subscription.user == current_user) || already_sent_mail)
          UserMailer.with(comment:, subscription:, from_user: current_user).got_new_comment_for_subscription.deliver_later
          already_sent_mail = true
        end
      end
    end
  end
end
