class CommentsController < ApplicationController
  before_action :require_login
  before_action :set_commentable

  def create
    @comment = @commentable.comments.build(comment_params)
    @comment.user = current_user

    if @comment.save
      respond_to do |format|
        format.turbo_stream
        format.html { redirect_to @commentable }
      end
    else
      # Handle validation errors
      redirect_to @commentable, alert: "Comment could not be created: #{@comment.errors.full_messages.to_sentence}"
    end
  end

  private

  def set_commentable
    if params[:entry_id]
      @commentable = Entry.find(params[:entry_id])
    end
  end

  def comment_params
    params.require(:comment).permit(:body, :language_code)
  end
end
