
class CountriesController < ApplicationController
  before_action :user_is_admin?
  before_action :set_country, only: %i[ show edit update destroy ]
  layout "admin"

  def index
    @countries = Country.all
  end

  def show
  end

  def new
    @country = Country.new
  end

  def edit
  end

  def create
    @country = Country.new(country_params)

    if @country.save
      redirect_to @country, notice: "Country creado."
    else
      render :new, status: :unprocessable_entity
    end
  end

  def update
    if @country.update(country_params)
      redirect_to @country, notice: "Country actualizado."
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @country.destroy
    redirect_to countries_url, notice: "Country eliminado."
  end

  private
    def set_country
      @country = Country.find(params[:id])
    end

    def country_params
      params.require(:country).permit(:continent, :sub_continent, :name, :code, :alpha_2, :alpha_3, :active)
    end
end
