# frozen_string_literal: true

class ChecklistsController < ApplicationController
  include BackofficeMenu

  before_action :set_checklist, only: %i[show edit update destroy]

  # GET /checklists
  def index
    @checklists = Checklist.all
  end

  # GET /checklists/1
  def show; end

  # GET /checklists/new
  def new
    @checklist = Checklist.new
  end

  # GET /checklists/1/edit
  def edit; end

  # POST /checklists
  def create
    @checklist = Checklist.new(checklist_params)

    if @checklist.save
      redirect_to @checklist, notice: "Checklist was successfully created."
    else
      render :new, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /checklists/1
  def update
    if @checklist.update(checklist_params)
      redirect_to @checklist, notice: "Checklist was successfully updated.", status: :see_other
    else
      render :edit, status: :unprocessable_entity
    end
  end

  # DELETE /checklists/1
  def destroy
    @checklist.destroy!
    redirect_to checklists_url, notice: "Checklist was successfully destroyed.", status: :see_other
  end

  private

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

  # Only allow a list of trusted parameters through.
  def checklist_params
    params.require(:checklist).permit(:code, :name, :description, :description_html,
                                      check_ids: [],
                                      checklist_entries_attributes: %i[id check_id position _destroy])
  end
end
