class KnowledgeBasesController < ApplicationController
  before_action :set_knowledge_base, only: %w[show update destroy]

  rescue_from ActiveRecord::StaleObjectError do |e|
    render json: {
      errors: {
        model: @knowledge_base.model_name.singular,
        params: knowledge_base_params,
        record: @knowledge_base.reload,
        attempted_action: e.attempted_action
      }
    }, status: :conflict
  end

  def index
    knowledge_bases = []
    KnowledgeBase.all.find_each do |knowledge_base|
      knowledge_bases << serialize(knowledge_base)
    end
    render json: knowledge_bases
  end

  def create
    knowledge_base = KnowledgeBase.new(knowledge_base_params)

    if knowledge_base.save
      render json: serialize(knowledge_base.reload), status: :created
    else
      render json: knowledge_base.errors, status: :unprocessable_entity
    end
  end

  def show
    render json: serialize(@knowledge_base)
  end

  def update
    if @knowledge_base.update(knowledge_base_params)
      render json: serialize(@knowledge_base.reload)
    else
      render json: @knowledge_base.errors, status: :unprocessable_entity
    end
  end

  def destroy
    @knowledge_base.destroy
    head :no_content
  end

  private

  def serialize(knowledge_base)
    return unless knowledge_base

    KnowledgeBaseSerializer.render_as_hash(knowledge_base)
  end

  def set_knowledge_base
    @knowledge_base = KnowledgeBase.find(params[:id])
  end

  def knowledge_base_params
    params.fetch(:knowledge_base, {}).permit(:name, :author, :contributors, :lock_version)
  end
end
