# frozen_string_literal: true

class ChecklistEntriesController < ApplicationController
  before_action :set_checklist_entry, only: %i[show edit update destroy]

  # GET /checklist_entries
  def index
    @checklist_entries = ChecklistEntry.all
  end

  # GET /checklist_entries/1
  def show; end

  # GET /checklist_entries/new
  def new
    @checklist_entry = ChecklistEntry.new(checklist_id: params[:checklist_id])
  end

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

  # POST /checklist_entries
  def create
    @checklist_entry = ChecklistEntry.new(checklist_entry_params)

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

  # PATCH/PUT /checklist_entries/1
  def update
    if @checklist_entry.update(checklist_entry_params)
      respond_to do |format|
        format.turbo_stream
        format.html do
          redirect_to @checklist_entry.checklist, notice: "Checklist entry was successfully updated.",
                                                  status: :see_other
        end
      end
    else
      render :edit, status: :unprocessable_entity
    end
  end

  # DELETE /checklist_entries/1
  def destroy
    @checklist_entry.destroy!
    respond_to do |format|
      format.html do
        redirect_to checklist_entries_url, notice: "Checklist entry was successfully destroyed.", status: :see_other
      end
      format.turbo_stream
    end
  end

  private

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

  # Only allow a list of trusted parameters through.
  def checklist_entry_params
    params.require(:checklist_entry).permit(:checklist_id, :check_id, :position)
  end
end
