# frozen_string_literal: true

class SuccessCriteriaController < ApplicationController
  before_action :set_element, only: %i[new create index new_from_checklist create_from_checklist]
  before_action :set_success_criterion, only: %i[show edit update destroy]

  # GET /success_criteria
  def index
    @success_criteria = SuccessCriterion.all
  end

  # GET /success_criteria/1
  def show; end

  # GET /success_criteria/new
  def new
    @success_criterion = SuccessCriterion.new(element: @element)
  end

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

  # POST /success_criteria
  def create
    check_id = success_criterion_params.delete(:check_id)
    check = Check.find(check_id)
    merge_params = {
      element: @element,
      title: check.name_de,
      quick_criterion: check.quick_criterion_de,
      quick_fail: check.quick_fail_de,
      quick_fix: check.quick_fix_de
    }
    @success_criterion = SuccessCriterion.new(success_criterion_params.merge(merge_params))

    if @success_criterion.save
      respond_to do |format|
        format.html do
          redirect_to @success_criterion, notice: "Erfolgskriterium was successfully created."
        end
        format.turbo_stream
      end
    else
      Rails.logger.debug(@success_criterion.errors)
      render :new, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /success_criteria/1
  def update
    if @success_criterion.update(success_criterion_params)
      redirect_to @success_criterion, notice: "Erfolgskriterium was successfully updated.", status: :see_other
    else
      render :edit, status: :unprocessable_entity
    end
  end

  # DELETE /success_criteria/1
  def destroy
    @success_criterion.destroy!
    respond_to do |format|
      format.html do
        redirect_to success_criteria_url, notice: "Erfolgskriterium was successfully destroyed.", status: :see_other
      end
      format.turbo_stream
    end
  end

  def new_from_checklist
    @success_criterion = SuccessCriterion.new(element: @element)
  end

  def create_from_checklist
    checklist = Checklist.find(params[:checklist_id])
    success = false
    SuccessCriterion.transaction do
      @success_criteria = checklist.checks.map do |check|
        SuccessCriterion.create!(check:,
                                 element: @element,
                                 title: check.name_de,
                                 quick_criterion: check.quick_criterion_de,
                                 quick_fail: check.quick_fail_de,
                                 quick_fix: check.quick_fix_de,
                                 level: check.conformity_level)
      end
      success = true
    end
    if success
      respond_to do |format|
        format.html do
          redirect_to @success_criteria.first.element, notice: "Erfolgskriterium was successfully created."
        end
        format.turbo_stream
      end
    else
      Rails.logger.debug(@success_criterion.errors)
      render :new, status: :unprocessable_entity
    end
  end

  private

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

  # Only allow a list of trusted parameters through.
  def success_criterion_params
    params.require(:success_criterion).permit(:element_id, :title, :quick_criterion, :quick_fail, :quick_fix, :priority, :level, :result, :test_comment, :check_id)
  end

  def set_element
    @element = Element.find(params[:element_id])
  end
end
