# frozen_string_literal: true

class ChecksController < ApplicationController
  include BackofficeMenu

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

  # GET /checks or /checks.json
  def index
    @pagy, @checks = pagy(Check.filter_by(filter_params).order(:external_number))
  end

  # GET /checks/1 or /checks/1.json
  def show; end

  # GET /checks/new
  def new
    @check = Check.new
  end

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

  # POST /checks or /checks.json
  def create
    @check = Check.new(check_params)

    respond_to do |format|
      if @check.save
        format.html do
          redirect_to check_url(@check),
          notice: t("scaffold.model_created_successfully", model: @check.model_name.human)
        end
        format.json { render :show, status: :created, location: @check }
      else
        flash[:alert] = t("there_were_errors", count: @check.errors.size)
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @check.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /checks/1 or /checks/1.json
  def update
    respond_to do |format|
      if @check.update(check_params)
        format.html do
          redirect_to check_url(@check),
                      notice: t("scaffold.model_updated_successfully", model: @check.model_name.human)
        end
        format.json { render :show, status: :ok, location: @check }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @check.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /checks/1 or /checks/1.json
  def destroy
    @check.destroy!

    respond_to do |format|
      format.html do
        redirect_to checks_url, notice: t("scaffold.model_destroyed_successfully", model: @check.model_name.human)
      end
      format.json { head :no_content }
    end
  end

  def filter_params
    @filter_params ||= params.permit(filter: [ :s, priority: [], conformity_level: [], standard_ids: [], principle_id: [], target_disabilities: []])[:filter] || {}
  end

  private

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

  # Only allow a list of trusted parameters through.
  def check_params
    params.require(:check).permit(:principle_id,
                                  :number,
                                  :name_de,
                                  :name_en,
                                  :standard_id,
                                  :visual,
                                  :auditory,
                                  :physical,
                                  :cognitive,
                                  :applicable_to_app,
                                  :applicable_to_web,
                                  :applicable_to_analogue,
                                  :applicable_to_document,
                                  :applicable_to_non_web,
                                  :external_number,
                                  :external_url,
                                  :conformity_level,
                                  :conformity_notice_de,
                                  :conformity_notice_en,
                                  :priority,
                                  :quick_criterion_de,
                                  :quick_criterion_en,
                                  :quick_fail_de,
                                  :quick_fail_en,
                                  :quick_fix_de,
                                  :quick_fix_en,
                                  :criterion_de,
                                  :criterion_en,
                                  :criterion_details_de,
                                  :criterion_details_en,
                                  :example_de,
                                  :example_en,
                                  :annotation_de,
                                  :annotation_en,
                                  :exemption_details_de,
                                  :exemption_details_en,
                                  :standard_text_de,
                                  :standard_text_en,
                                  :test_instructions,
                                  :powerpoint_text_de,
                                  :powerpoint_text_en,
                                  :comment,
                                  link_ids: [],
                                  standard_ids: [])
  end
end
