class DocumentsController < ApplicationController
  before_action :set_document, only: %i[show edit update destroy]

  # GET /documents or /documents.json
  def index
    @documents = policy_scope(Document)
  end

  # GET /documents/1 or /documents/1.json
  def show
  end

  # GET /documents/new
  def new
    @document = Document.new
    authorize @document
  end

  # GET /documents/1/edit
  def edit
  end

  # POST /documents or /documents.json
  def create
    @document = Document.new(document_params)
    authorize @document

    respond_to do |format|
      if @document.save
        logger.info "[PRIVILEGED] Document(#{@document.id}) created by #{current_user.id}"
        format.html { redirect_to document_url(@document, locale: I18n.locale), notice: "Document was successfully created." }
        format.json { render :show, status: :created, location: @document }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /documents/1 or /documents/1.json
  def update
    respond_to do |format|
      if @document.update(document_params)
        logger.info "[PRIVILEGED] Document(#{@document.id}) updated by #{current_user.id}"
        format.html { redirect_to document_url(@document, locale: I18n.locale), notice: "Document was successfully updated." }
        format.json { render :show, status: :ok, location: @document }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /documents/1 or /documents/1.json
  def destroy
    @document.destroy!
    logger.info "[PRIVILEGED] Document(#{@document.id}) destroyed by #{current_user.id}"

    respond_to do |format|
      format.html { redirect_to documents_url, notice: "Document was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_document
    @document = Document.find(params[:id])
    authorize @document
  end

  # Only allow a list of trusted parameters through.
  def document_params
    params.fetch(:document, {}).permit :url, :title, :description
  end
end
