# frozen_string_literal: true

class MessagesController < ApplicationController
  respond_to :html, :json
  before_action :authenticate_member!
  def index
    redirect_to conversations_path(box: @box)
  end

  def show
    if (@message = Message.find_by(id: params[:id])) && (@conversation = @message.conversation)
      if @conversation.is_participant?(current_member)
        redirect_to conversation_path(@conversation, box: @box, anchor: "message_" + @message.id.to_s)
        return
      end
    end
    redirect_to conversations_path(box: @box)
  end

  def new
    return if params[:recipient_id].blank?

    @recipient = Member.find_by(id: params[:recipient_id])
    nil if @recipient.nil?
  end

  def create
    if params[:conversation_id].present?
      @conversation = Mailboxer::Conversation.find(params[:conversation_id])
      # Check if any of the recipients have blocked the sender
      if @conversation.recipients.any? { |recipient| recipient.already_blocking?(current_member) }
        flash[:error] = "You cannot reply to this conversation because one of the recipients has blocked you."
        redirect_to conversation_path(@conversation)
        return
      end
      current_member.reply_to_conversation(@conversation, params[:body])
      redirect_to conversation_path(@conversation)
    else
      recipient = Member.find(params[:recipient_id])
      if recipient.already_blocking?(current_member)
        flash[:error] = "You cannot send a message to a member who has blocked you."
        redirect_back_or_to(root_path)
        return
      end
      body = params[:body]
      subject = params[:subject]
      @conversation = current_member.send_message(recipient, body, subject)
      redirect_to conversations_path(box: 'sentbox')
    end
  end
end
