class CommandsController < ApplicationController
  before_action :set_command, only: %i[ show edit update destroy run ]

  # GET /commands or /commands.json
  def index
    authorize @commands = Command.all
  end

  # GET /commands/1 or /commands/1.json
  def show
    authorize @command
  end
  
  # RUN /commands/1/run
  def run
    authorize @command
    response_text = eval(@command.body)
    redirect_to (request.referrer || @command), notice: response_text
  end


  # GET /commands/new
  def new
    @command = Command.new
  end

  # GET /commands/1/edit
  def edit
    authorize @command
  end

  # POST /commands or /commands.json
  def create
    @command = Command.new(command_params)

    respond_to do |format|
      if @command.save
        format.html { redirect_to command_url(@command), notice: "Command was successfully created." }
        format.json { render :show, status: :created, location: @command }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @command.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /commands/1 or /commands/1.json
  def update
    authorize @command
    respond_to do |format|
      if @command.update(command_params)
        format.html { redirect_to command_url(@command), notice: "Command was successfully updated." }
        format.json { render :show, status: :ok, location: @command }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @command.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /commands/1 or /commands/1.json
  def destroy
    @command.destroy

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

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_command
      @command = Command.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def command_params
      params.require(:command).permit(:name, :trigger, :body)
    end
end
