module Admin
  class CategoriesController < Admin::AdminController
    before_action :set_category, only: %i[show edit update destroy]

    def index
      @categories = Category.all
      @title = admin_title
    end

    def show
      redirect_to %i[admin categories]
    end

    def new
      @category = Category.new
      @title    = admin_title
    end

    def edit
      @title = admin_title(@category, %i[id name])
    end

    def create
      @category = Category.new(category_params)

      if @category.save
        redirect_to [:admin, @category], notice: t('.notice')
      else
        render :new
      end
    end

    def update
      if @category.update(category_params)
        redirect_to [:admin, @category], notice: t('.notice')
      else
        render :edit
      end
    end

    def destroy
      @category.destroy
      redirect_to %i[admin categories], notice: t('.notice')
    end

    private

    def set_category
      @category = Category.find(params[:id])
    end

    def category_params
      params.expect category: %i[name slug]
    end
  end
end
