# typed: true
# frozen_string_literal: true

module Admin
  class ProductsController < ApplicationController
  before_action :require_admin
  before_action :set_product, only: [ :edit, :update, :destroy ]

  def index
    @products = Product.order(active: :desc, name: :asc)
  end

  def new
    @product = Product.new
  end

  def create
    @product = Product.new(product_params)

    if @product.save
      redirect_to admin_products_path, notice: "Product was successfully created."
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @product.update(product_params)
      redirect_to admin_products_path, notice: "Product was successfully updated."
    else
      render :edit
    end
  end

  def destroy
    @product.update(active: false)
    redirect_to admin_products_path, notice: "Product was successfully deactivated."
  end

  private

  def set_product
    @product = Product.find(params[:id])
  end

  def product_params
    params.require(:product).permit(:name, :price, :currency, :days, :active)
  end
  end
end
