class CanistersController < ApplicationController
  before_action :set_canister, only: %i[ show edit update destroy ]

  # GET /canisters or /canisters.json
  def index
    authorize @canisters = policy_scope(Canister)
  end

  # GET /canisters/1 or /canisters/1.json
  def show
    authorize @canister
  end

  # GET /canisters/new
  def new
    authorize @canister = current_user.canisters.new
  end

  # GET /canisters/1/edit
  def edit
    authorize @canister
  end

  # POST /canisters or /canisters.json
  def create
    authorize @canister = Canister.new(canister_params)

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

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

  # DELETE /canisters/1 or /canisters/1.json
  def destroy
    authorize @canister
    @canister.destroy
    respond_to do |format|
      format.html { redirect_to canisters_url, notice: "Canister was successfully destroyed." }
      format.json { head :no_content }
    end
  end

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

    # Only allow a list of trusted parameters through.
    def canister_params
      params.require(:canister).permit(:icid, :name, :user_id)
    end
end
